2010-06-10 66 views
2

我有一个ASP.NET MVC POST操作,用于在提交表单时保存实体。它适用于插入,但不适用于更新,数据库不会被调用,所以它显然没有跟踪更改,因为它是“分离的”。我使用实体框架瓦特/ .NET 4:ASP.NET MVC&Detached Entity不会保存

//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"); 
     } 

任何想法,将不胜感激,谢谢,

贾斯汀

回答

1

你永远适用的新数据的上下文。您可以使用Stub Entity技巧来避免查询。

尝试是这样的(从内存中,SRY如果有错误):

public ActionResult Save(Developer developer) 
{ 
    developer.UpdateDate = DateTime.Now; 

    Developer d = new Developer { ID = developer.ID }; 
    DataContext.Attach(d); 

    DataContext.ApplyCurrentValues(developer); 
    DataContext.SaveChanges(); 
} 
+1

只需注意,ApplyCurrentValues适用于.NET 4.在4之前,请使用ApplyPropertyChanges(http://msdn.microsoft.com/zh-cn/library/system.data.objects.objectcontext.applypropertychanges.aspx) – 2010-06-10 12:02:44

-1

如果它断开,必须重新连接它:

MSDN: Attaching and Detaching Objects (Entity Framework)

你可能想:

[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"); 
    } 
+0

为什么downvote? – jfar 2010-06-10 03:54:16

0

的信息,请参阅here上更新分离对象,但下面应该让你在那里(作为其他块if(developer.DeveloperID == 0)):

else 
{ 
    var original = DataContext.DeveloperData 
           .Where(d => d.DeveloperID == developer.DeveloperID) 
           .FirstOrDefault(); 

    DataContext.DeveloperData.Attach(developer); 
    context.ApplyOriginalValues(original); 
} 
1

对不起张贴到旧的线程,但我只是碰到了这个问题,我找到了解决办法是一个比较有效的这些我相信。问题在于,当您将实体附加到上下文时,实体EntityState将重置为“未更改”。所以你必须回到“修改”状态。最简单的方法是将属性设置为自己。

//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"); 
    } 

这些更改都应该解决更新问题。

我仍然在寻找的原因是,这对我来说似乎是一种破绽,而且必须有一种“更干净”的方式才能做到。不过我觉得这是所提供的解决方案中最便宜的。我有点震惊,设置一个属性本身触发了属性更改事件。我认为它会检查以确保值不相同。