How to find out which entity is inside the ObjectStateManager more then once

五迷三道 提交于 2019-12-11 10:37:34

问题


Hi i get the following error:

AcceptChanges cannot continue because the object's key values conflict with another object in the ObjectStateManager. Make sure that the key values are unique before calling AcceptChanges.

Now i'm trying to fix it but i need to pinpoint what entity is giving me trouble so i know where to look.

i know what the error is and i know how to solve it, but i don't know how to find out which entity is double.


回答1:


You can check the ObjectStateManager to get all the modified entities. the code goes something like this:

var lst = context.ObjectStateManager
              .GetObjectStateEntries(EntityState.Added |
                                     EntityState.Modified | 
                                     EntityState.Deleted);
var res = lst.GroupBy(c => c.EntityKey).Where(c=> c.Count() > 1);


来源:https://stackoverflow.com/questions/14834687/how-to-find-out-which-entity-is-inside-the-objectstatemanager-more-then-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!