ASP.NET MVC & Detached Entity won't save
I have an ASP.NET MVC POST action for saving an entity on submit of a form. It works fine for insert but doesn't work for update, the database doesn't get called, so it's clearly not tracking the changes, as it's "detached". I'm using Entity Framework w/.NET 4:
//POST: /Developers/Save/
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Save(Developer developer)
{
developer.UpdateDate = DateTime.Now;
if (developer.DeveloperID == 0)
{//inserting new developer.
DataContext.DeveloperData.Insert(developer);
}
//save changes - TODO: doesn't update...
DataContext.SaveChanges();
//redirect to developer list.
return RedirectToAction("Index");
}
Any id开发者_Go百科eas would be greatly appreciated, thanks,
Justin
You're never applying the new data to the context. You can use the Stub Entity trick to avoid querying.
Try something like this (from memory, sry if there are errors):
public ActionResult Save(Developer developer)
{
developer.UpdateDate = DateTime.Now;
Developer d = new Developer { ID = developer.ID };
DataContext.Attach(d);
DataContext.ApplyCurrentValues(developer);
DataContext.SaveChanges();
}
Sorry for posting to an older thread but I just ran into this issue and the solution I found is a bit more efficient than these I believe. The problem is that when you attach an entity back to the context the entities EntityState is reset to "Unchanged". So all you have to get that back to the "Modified" state. The easiest way to do that is to set a property to itself.
//POST: /Developers/Save/
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Save(Developer developer)
{
developer.UpdateDate = DateTime.Now;
if (developer.DeveloperID == 0)
{//inserting new developer.
DataContext.DeveloperData.Insert(developer);
}else{
DataContext.Attach(developer);
}
**developer.DeveloperID = developer.DeveloperID;
// Just ran into a problem that using the primary key like this doesn't reset the EntityState... but using string fields does change it so take that into consideration.
// OR IN YOUR CASE YOU CAN JUST MOVE DOWN THE UpdateDate set statement above like:
developer.UpdateDate = DateTime.Now;**
//save changes
DataContext.SaveChanges();
//redirect to developer list.
return RedirectToAction("Index");
}
Either of those changes should fix the update problem.
The reason I was still looking is this seems like a hack to me and there has to be a "cleaner" way to do it. Still I feel it's the least expensive of the solutions provided. I was a bit shocked that setting a property to itself triggered the property change event. I assumed it would check to make sure the values were not identical.
See here for info on updating detached objects, but the following should get you there (as the else block for if(developer.DeveloperID == 0)
):
else
{
var original = DataContext.DeveloperData
.Where(d => d.DeveloperID == developer.DeveloperID)
.FirstOrDefault();
DataContext.DeveloperData.Attach(developer);
context.ApplyOriginalValues(original);
}
If its disconnected, you have to re-attach it:
MSDN: Attaching and Detaching Objects (Entity Framework)
You probably want:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Save(Developer developer)
{
developer.UpdateDate = DateTime.Now;
if (developer.DeveloperID == 0)
{//inserting new developer.
DataContext.Attach(developer);
}
//save changes - TODO: doesn't update...
DataContext.SaveChanges();
//redirect to developer list.
return RedirectToAction("Index");
}
精彩评论