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

九州編程
TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超4個(gè)贊
看錯(cuò)誤信息的意思是EF上下文中已經(jīng)有當(dāng)前這個(gè)實(shí)體了,所以不需要AttachTo,你把AttachTo這一行去掉試試
- 2 回答
- 0 關(guān)注
- 503 瀏覽
添加回答
舉報(bào)
0/150
提交
取消