我有以下编辑方法:
[HttpPost]
public ActionResult Edit(Movie movie)
{
try
{
_db.ApplyCurrentValues("Movies1",movie);
_db.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
运行时我收到以下错误:
An object with a key that matches the key of the supplied object could not be found in the ObjectStateManager. Verify that the key values of the supplied object match the key values of the object to which changes must be applied.
几点:
答案 0 :(得分:2)
为了应用当前值,具有该给定键的实体应存在于ObjectStateManager
中。 ApplyCurrentValues州的文档
将提供的对象中的标量值复制到对象中 具有相同键的ObjectContext。
您可以附加实体并应用当前值。
_db.Movies.Attach(movie);
_db.ObjectStateManager.ChangeState(movie, EntityState.Modified);
_db.SaveChanges();