Can an entity be unloaded from entity framework's ObjectStateManager?

末鹿安然 提交于 2019-12-12 18:19:59

问题


I am using Entity Framework with Self-Tracking Entities over WCF.

I have 2 entities, Customer and PriceMatrix with a many-to-many relationship (the customer can have many historic price matrices, and a matrix can be shared between a group of customers)

The client has a single PriceMatrix which it updates and sends back to the server. The server needs to check if this PriceMatrix is shared among other customers before choosing to either update it, or duplicate it and update the duplicate.

The problem I have is that because I am loading the PriceMatrix to examine it before I attach the updated PriceMatrix I am getting the following error when I call ApplyChanges().

"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."

If I comment out my checks (and consequently don't load the PriceMatrix) everything works fine.

What I really want to do is, after examining its state, unload the PriceMatrix and then attach the updated PriceMatrix I received from the client.


回答1:


In such case try to use Detach method of the ObjectContext. This is known issue in STEs - AcceptChanges requires that STE with the same key is not previously loaded into context.



来源:https://stackoverflow.com/questions/4874671/can-an-entity-be-unloaded-from-entity-frameworks-objectstatemanager

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