Hey all,
I'm trying to update a detached entity in .NET 4 EF:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Save(Developer developer)
{
developer.UpdateDate = DateTime.Now;
if (developer.DeveloperID == 0)
{//inserting new developer.
DataContext.DeveloperData.Insert(developer);
}
else
{//attaching existing developer.
DataContext.DeveloperData.Attach(developer);
}
//save changes.
DataContext.SaveChanges();
//redirect to developer list.
return RedirectToAction("Index");
}
public static void Attach(Developer developer)
{
var d = new Developer { DeveloperID = developer.DeveloperID };
db.Developers.Attach(d);
db.Developers.ApplyCurrentValues(developer);
}
However, this gives the following error:
An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.
Anyone know what I'm missing?
Thanks, Justin