注意Entity中的Uptate
生活随笔
收集整理的這篇文章主要介紹了
注意Entity中的Uptate
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
今天在調用架構中的UpdateEntity方法時
public bool UpdateEntity(T entity)
{
OC.CreateObjectSet<T>().Attach(entity);//這就是報錯所在
OC.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
//return db.SaveChanges() > 0;
return true;
}
報ObjectStateManager 中已存在具有同一鍵的對象。ObjectStateManager 無法跟蹤具有相同鍵的多個對象。錯誤
原因是我是這么調用的
model.djbh = zzService.GetEntity(zz => zz.ygzzid == model.ygzzid).djbh;
if (zzService.UpdateEntity(model))
{
return Content("edit");
}
else
{
return Content("no");
}
我先在entity中找出對應的model了,這樣ObjectStateManager 實際是已跟蹤這個實體
轉載于:https://www.cnblogs.com/xjt360/p/3992790.html
總結
以上是生活随笔為你收集整理的注意Entity中的Uptate的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MVC初学 - The type or
- 下一篇: 【Socket】关于socket长连接的