views:

3370

answers:

4

Hi

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.

A: 

"TryGetObjectStateEntry(Contact, out contact)" will attach the object from the database if it is not currently there in the context. So "DataContext.Attach" will throw an error because its already attached

ravi
So?Do you have any idea???
Shimmy
ObjectStateManager.TryGetObjectStateEntry(Object, ObjectStateEntry) does not attach the object from the database if it is not currently in the context. You can call it multiple times and if the ObjectStateEntry is not found it will continue to return false every time. I've run a test and after multiple calls to TryGetObjectStateEntry(Object, ObjectStateEntry) a call to ObjectContext.Attach(IEntityWithKey) will work.
Daniel Richardson
you are right. I was confusing TryGetObjectStateEntry with TryGetObjectByKey.
ravi
+2  A: 

Could your Contact entity have two child entities with the same EntityKey? For example, is it possible to get from the Contact entity to two Address entities with the same key?

If you specify MergeOptions.NoTracking a context will happily return a detached object graph that contains entities with the same key. However, when you attach the same object graph a System.InvalidOperationException will be thrown.

I would suggest that you look at the entire object graph that you are attaching to the context and check if there are objects with duplicate keys in it.

Daniel Richardson
+2  A: 

Hello, I had experienced the same problem within my application.

I have solved the problem by using ObjectStateManager TryGetObjectStateEntry Method

In fact the EntityState property is misleading developers. Although it is displaying Detached, interesting that causing error.

A: 

Answer is (and I didn't mention that this was the problem, since I didn't know it is), that if you set a navigation property to a tracked entity the new entity is automatically added:

Dim s = context.States.FirstOrDefault()
Dim a As New Address
a.State = s

Dim state = a.EntityState '= Added

Since I didn't know that I kept on wondering how come the entity is tracked. I would of deleted this answer but since there is effort of other answer that might be helpful I will leave it here, vote to close if you think it should be closed.

Shimmy