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 }
錯(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 回答

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