I've been stuck with this problem for over a week now. Hopefully some one can point me in the right direction.
I start with a brief description of my schema.
Asset 1--->1 Address *-->1 Area *-->1 Region *-->1 Country
Package 1-->* Asset
Using Self Tracking Entity (STE) + WCF.
Steps:
In step 2, the call uses eager loading of Addresses.
from p in context.Assets.Include("Address.Area.Region.Country")
This is the error when attempting to call
context.Packages.ApplyChanges(package)
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.
EDIT
After snooping around, i found that this is a STE issue. The problem being you cannot persist a graph that contains multiple instances of the same entity as outlined here. Here is my question.
How can I add an entity to my entity collection. The new entity may have related entities that contain the same key as one already in the collection. I.e. Add a new asset that may contain the same address, area, region or country entity.
Here are my constrains:
I am aware of the possible solutions suggested by Diego B Vega, but those are not options i can use for my solution. Has anyone any other ideas?
[SOLVED] => AcceptChanges is unable to proceed because the object's... AcceptChanges is unable to proceed because the object's key values contradict with those of another object in the ObjectStateManager. I've been stuck with this problem for over a week now.
Self-Tracking Entities (STEs) can help you track changes in any tier and then replay these changes into a context to be saved. Use STEs only if the context is not available on a tier where the changes to the object graph are made. If the context is available, there is no need to use STEs because the context will take care of tracking changes.
(If the entity already has a generated key value, then it is tracked as Unchanged instead.) Removing an entity from DbSet.Local causes it to be marked as Deleted.
DbContext.Entry returns an EntityEntry<TEntity> instance for a given entity instance. ChangeTracker.Entries returns EntityEntry<TEntity> instances for all tracked entities, or for all tracked entities of a given type.
FYI, I wrote a blog post with some additional suggestions to what I had already responded in the EF Forums. Here is the blog post.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With