通过 MVC 中的 ApplyPropertyChanges 对 EF 模型应用更改时出现异常
我尝试保存已编辑的实体框架实体ApplyPropertyChanges,并收到异常:
“ObjectStateManager 不 包含一个 ObjectStateEntry 和 对类型对象的引用 'MvcApplication1.Models.Product'。"} System.Exception {系统.InvalidOperationException}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Product productToEdit) //all properties of Product are valid
{
try
{
productsDBEntities.ApplyPropertyChanges("ProductSet", productToEdit); //exception here
entities.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
任何想法将不胜感激!
谢谢!
I try to save an edited Entity Framework entity ApplyPropertyChanges, and get an exception:
"The ObjectStateManager does not
contain an ObjectStateEntry with a
reference to an object of type
'MvcApplication1.Models.Product'."} System.Exception
{System.InvalidOperationException}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Product productToEdit) //all properties of Product are valid
{
try
{
productsDBEntities.ApplyPropertyChanges("ProductSet", productToEdit); //exception here
entities.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
Any idea would be very much appreciated!
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想使用
ApplyPropertyChanges
,您必须首先从数据库加载Product
:或者您可以使用另一种方法:
顺便说一句。您使用的是.NET 3.5吗?
ApplyPropertyChanges
在 .NET 4.0 中已过时。You must first load
Product
from database if you want to useApplyPropertyChanges
:Or you can use another approach:
Btw. are you using .NET 3.5?
ApplyPropertyChanges
is obsolete in .NET 4.0.