Entity Framework Code-First: & ldquo; ObjectStateManager can not track multiple objects with the same key. & Quot;

advertisements

I'm running into an issue with Entity Framework code-first in MVC3. I'm hitting this exception:

An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.

This is addressed many times on SO, but I'm having trouble utilizing any of the suggested solutions in my situation.

Here is a code sample:

FestORM.SaleMethod method = new FestORM.SaleMethod
{
    Id = 2,
    Name = "Test Sale Method"
};
FestContext context = new FestContext();

//everything works without this line:
string thisQueryWillMessThingsUp =
    context.SaleMethods.Where(m => m.Id == 2).Single().Name;

context.Entry(method).State = System.Data.EntityState.Modified;
 context.SaveChanges();

EDITED to clarify: I am attempting to update an object that already exists in the database.

Everything works fine without the query noted in the code. In my application, my controller is instantiating the context, and that same context is passed to several repositories that are used by the controller--so I am not able to simply use a different context for the initial query operation. I've tried to remove the entity from being tracked in the ObjectStateManager, but I can't seem to get anywhere with that either. I'm trying to figure out a solution that will work for both conditions: sometimes I will be updating an object that is tracked by the ObjectStateManager, and sometimes it will happen to have not been tracked yet.

FWIW, my real repository functions look like this, just like the code above:

public void Update(T entity)
{
    //works ONLY when entity is not tracked by ObjectStateManager
    _context.Entry(entity).State = System.Data.EntityState.Modified;
}

public void SaveChanges()
{
    _context.SaveChanges();
}

Any ideas? I've been fighting this for too long...


The problem is that this query

string thisQueryWillMessThingsUp =
    context.SaleMethods.Where(m => m.Id == 2).Single().Name;

brings one instance of the SaleMethod entity into the context and then this code

context.Entry(method).State = System.Data.EntityState.Modified;

attaches a different instance to the context. Both instances have the same primary key, so EF thinks that you are trying to attach two different entities with the same key to the context. It doesn't know that they are both supposed to be the same entity.

If for some reason you just need to query for the name, but don't want to actually bring the full entity into the context, then you can do this:

string thisQueryWillMessThingsUp =
    context.SaleMethods.Where(m => m.Id == 2).AsNoTracking().Single().Name;

If what you are tying to do is update an existing entity and you have values for all mapped properties of that entity, then the simplest thing to do is to not run the query and just use:

context.Entry(method).State = System.Data.EntityState.Modified;

If you don't want to update all properties, possibly because you don't have values for all properties, then querying for the entity and setting properties on it before calling SaveChanges is an acceptable approach. There are several ways to do this depending on your exact requirements. One way is to use the Property method, something like so:

var salesMethod = context.SaleMethods.Find(2); // Basically equivalent to your query
context.Entry(salesMethod).Property(e => e.Name).CurrentValue = newName;
context.Entry(salesMethod).Property(e => e.SomeOtherProp).CurrentValue = newOtherValue;
context.SaveChanges();

These blog posts contain some additional information that might be helpful:

http://blogs.msdn.com/b/adonet/archive/2011/01/29/using-dbcontext-in-ef-feature-ctp5-part-4-add-attach-and-entity-states.aspx

http://blogs.msdn.com/b/adonet/archive/2011/01/30/using-dbcontext-in-ef-feature-ctp5-part-5-working-with-property-values.aspx