3

I have the following model:

Entity Model

In the database, suppose I have the user Administrator and an artifact which is created and modified by this user Administrator.

Data row

The code bellow is not working, and I would like to know what exactly is happening and how could I make it work? Important: When the artifact is created/edited by different users, it works like a charm.

using (var context = new ModelEntities())
{
    //Get an artifact created and edited by the user Administrator
    var artifact = context.Artifact
                          .AsNoTracking()
                          .First(a => a.Name == "Artifact created/edited by the same user");

    var createdUser = artifact.CreatedBy;
    Console.WriteLine("{0} created.", createdUser.Name); // Administrator created.

    var modifiedUser = artifact.ModifiedBy;
    Console.WriteLine("{0} modified.", modifiedUser.Name); // Administrator modified.

    context.Set<Artifact>().Attach(artifact); // **Exception! An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.**

    artifact.Name = "New Name";

    context.Entry(artifact).State = EntityState.Modified;
    context.SaveChanges();
}

It seems to be a Entity Framework limitation to have two entities of same types in two different navigation properties.

Notice that I've tried to create this sample as simple as possible, removing obvious fields commonly required by business rules (like a datetime for creation and modification).

natenho
  • 5,231
  • 4
  • 27
  • 52
  • (+1) as I can't see why this is happening. Is there a reason that you are using AsNoTracking()? I am not sure if there is much benefit if you are saving back to the database. http://blog.staticvoid.co.nz/2012/4/2/entity_framework_and_asnotracking. It would be interesting to know if the problem goes away after removing AsNoTracking and just calling SaveChanges without the Attach. – acarlon Sep 18 '13 at 02:52
  • Surprisely, striping the AsNoTracking() it works. Although it doesn't explain too much about the problem itself, does it? There is a lot of places which AsNoTracking has [benefits](http://blog.staticvoid.co.nz/2012/4/2/entity_framework_and_asnotracking). Without the AsNoTracking there are performance issues when the object cache has too many entities to deal with, for instance. – natenho Sep 18 '13 at 15:09
  • What if you call context.Set().AsNoTracking() before 'var artifact =' and keep everything else the same as your posted code? The reason that it works without AsNoTracking is because it has kept track that the artifact that you are modifying is the one that you extracted. The error you receive is that for some reason (possibly the position of AsNoTracking) it thinks that you are adding a new entry with the same name. – acarlon Sep 18 '13 at 23:55
  • Correction - 'name' should be 'key'. – acarlon Sep 19 '13 at 00:01
  • This link might also be helpful: http://social.msdn.microsoft.com/Forums/en-US/dbbde1ec-cafd-4c03-ba13-b2653e08cde3/attach-an-entity-to-the-dbcontext-that-was-retrieved-by-asnotracking – acarlon Sep 19 '13 at 00:19

0 Answers0