问题
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