I am trying to attach an entity to the ObjectContext. When I do so, the following InvalidOperationException is thrown:
An object with the same key already exists in the ObjectStateManager.
The ObjectStateManager cannot track multiple objects with the same key.
I checked in the object state manager and the item does not exist:
//Data context is actually the object context.
ObjectStateEntry contact;
while ( //Should only work once since it should be true if the item was attached
!DataContext.ObjectStateManager.
TryGetObjectStateEntry(Contact, out contact)
)
DataContext.Attach(Contact); //Here is the exception thrown.
Or look at this abstract example and tell me if it makes sense:
EntityState state = Contact.EntityState; //Detached
DataContext.Attach(Contact); //Throws the exception.
DataContext.AttachTo("Entities.Contacts", Contact); //Throws the Exception
var detached = DataContext.ObjectStateManager.
GetObjectStateEntries(EntityState.Detached);
//InvalidArgumentException - detached entities cannot be in the obj state mgr
Answers in VB are welcomed too.