How can I use strongly-typed Controllers with EntityObjects?
My failures...
First I tried this:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Guid id, Department Model)
{
db.SaveChanges();
return RedirectToAction("Index");
}
This failed to actually save any changes to the database. So, I tried to attach the model to my ObjectContext:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Guid id, Department Model)
{
db.Attach(Model);
db.SaveChanges();
return RedirectToAction("Index");
}
This failed because "an object with a null EntityKey value cannot be attached to an object context." So, I tried to assign the 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");
}
This failed because "an object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key."
How is this supposed to work?