【发布时间】:2009-05-21 22:26:31
【问题描述】:
如何将强类型控制器与 EntityObjects 一起使用?
我的失败...
首先我尝试了这个:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Guid id, Department Model)
{
db.SaveChanges();
return RedirectToAction("Index");
}
这未能实际保存对数据库的任何更改。因此,我尝试将模型附加到我的 ObjectContext:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Guid id, Department Model)
{
db.Attach(Model);
db.SaveChanges();
return RedirectToAction("Index");
}
这失败了,因为“无法将具有空 EntityKey 值的对象附加到对象上下文。”所以,我尝试分配EntityKey:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Guid id, Department Model)
{
Model.EntityKey = (from Department d in db.Department
where d.Id == id
select d).FirstOrDefault().EntityKey;
db.Attach(Model);
db.SaveChanges();
return RedirectToAction("Index");
}
这失败了,因为“ObjectStateManager 中已经存在具有相同键的对象。ObjectStateManager 无法跟踪具有相同键的多个对象。”
这应该如何工作?
【问题讨论】:
标签: c# .net asp.net asp.net-mvc entity-framework