Unable to attach a detached entity: "An object with the same key already exists in the ObjectStateManager"
Asked Answered
O

4

20

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.

Outstanding answered 15/7, 2009 at 14:34 Comment(0)
J
7

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.

Jequirity answered 10/8, 2009 at 19:50 Comment(1)
Im facing similar problem, can you provide example how would youlook at the entire object graph that you are attaching to the context and check if there are objects with duplicate keys in it?Adenectomy
O
5

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 delete the entire quesion 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.

Outstanding answered 15/11, 2009 at 0:48 Comment(1)
Related: #8760199Nation
I
4

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.

Iene answered 28/8, 2009 at 9:4 Comment(0)
H
0

Check whether you are setting the EntityKey property of Entity object. If you are setting it, Make sure you are not copying from an existing entity object.

Helmet answered 27/10, 2016 at 14:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.