从表单发布更新实体

时间:2017-02-01 09:03:23

标签: entity-framework

我正在尝试更新实体,如下所示 -

[HttpPost]
public ActionResult EditRa(RetailersAssistant ra)
{
    var existingRa = Db.RetailersAssistants.Find(ra.RaId);
    existingRa = ra;
    Db.SaveChanges();
    return RedirectToAction("RaList", "Admin");
}

但它没有更新数据库。也不例外。有什么帮助吗?

2 个答案:

答案 0 :(得分:1)

您应该将实体模式设置为"已修改"。请找到以下解决方案:

[HttpPost]         

public ActionResult EditRa(RetailersAssistant ra)

{    
var existingRa = Db.RetailersAssistants.Find(ra.RaId);

existingRa  = ra; // If it doesn't work then you can copy each property of ra to existingRa one by one.

Db.Entry(existingRa).State = System.Data.Entity.EntityState.Modified; 

Db.SaveChanges();

return RedirectToAction("RaList", "Admin");

}

答案 1 :(得分:0)

您需要更新existingRa实体的每个属性。现在,您只需分配新对象