1 /// <summary>
2 /// Save entity'propertity which is not null
3 /// </summary>
4 /// <typeparam name="T"></typeparam>
5 /// <param name="entity"></param>
6 public void SetModifyColumnsWithOutNull<T>(T entity) where T : EntityObject
7 {
8 ObjectStateEntry stateEntry = null;
9 bool isPresent = _ctx.ObjectStateManager.TryGetObjectStateEntry(entity, out stateEntry);
10 if (stateEntry == null)11 _ctx.AttachTo(typeof(T).Name, entity);12 }
錯誤:An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.說明:我是把上下文放在線程的CallContext里面了,在調(diào)用這個方法之前,我查詢過該對象,修改之后,調(diào)用這個方法進行更新,結(jié)果報上述錯誤。疑問:既然從上下文中獲取不到對象狀態(tài),那附加對象為何還是出錯呢?請大蝦們賜教小弟
2 回答

慕蓋茨4494581
TA貢獻1850條經(jīng)驗 獲得超11個贊
看錯誤信息的意思是EF上下文中已經(jīng)有當(dāng)前這個實體了,所以不需要AttachTo,你把AttachTo這一行去掉試試
- 2 回答
- 0 關(guān)注
- 690 瀏覽
添加回答
舉報
0/150
提交
取消