An entity object cannot be referenced by multiple instances of IEntityChangeTracker RRS feed

All replies

  • Hi scottz,


    Thanks for your links. I think @Julie is right, we should detach the entity from one Context before attach or add to the other Contexts.

    Have a nice day.

    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, April 25, 2012 1:42 PM
  • Hi scottz1;

    Have you tried first to Detach the object you wish to assign to a different context and then attached it to the second context.

    For example:
    // Two different context
    var ctx1 = new NorthwindEntities( );
    var ctx2 = new NorthwindEntities( );

    // Get a record into the first context
    var results = ctx1.Suppliers.FirstOrDefault();

    // Assign it to another context by first Detach it from first:
    // Then Attach it to the second


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, April 25, 2012 1:49 PM
  • Thanks Fernando. That's a helpful example. In my case, the parent record already exists, and I will create many new child records over time. I think I should Detach the parent record from the first context as soon as I look it up, and then Attach it to the second context each time I create a new child record, and then Detach it again from the second context after calling SaveChanges on every child record. Sound right?

    Wednesday, April 25, 2012 3:42 PM
  • I tried Detach after SaveChanges on every child. I'm getting the error: "An object with a temporary EntityKey value cannot be attached to an object context."

    This is the outline of the function that is called numerous times to create/save child records. I'm trying to avoid looking up the parent every time.

    using (Entities db = new Entities())
      if (cacheParent == null)
        cacheParent = db.Parents.FirstOrDefault();
        db.Parents.Attach(cacheParent);  // do this before using it with every child  

      var child = new ChildEntity();
      child.parent = cacheParent;          // assign fkey in child --> parent


      db.Parents.Detach(cacheParent);  // do this after using it with every child

    • Edited by scottz1 Wednesday, April 25, 2012 3:58 PM
    Wednesday, April 25, 2012 3:57 PM
  • I tried rewriting it like below, without Attach/Detach, but now I get the error "The INSERT statement conflicted with the FOREIGN KEY constraint". (The fkey constraint is the usual standard one that all child records inserted must have a valid parentID.) So I'm building the EntityKey object incorrectly, but I don't see how.

    Can someone please straighten me out in either this code, or the April 25 post snippet? And which way is faster? Thanks!

    using (Entities db = new Entities())
      if (cacheParent == null)
           cacheParent = db.Parents.FirstOrDefault();

      var child = new ChildEntity();
      IEnumerable<KeyValuePair<string, object>> entityKeyValues = new KeyValuePair<string, object>[]
         new KeyValuePair<string, object>("Id", cacheParent.Id)
      EntityKey key = new EntityKey("Entities.Parents", entityKeyValues);
      child.ParentReference.EntityKey = key;
      child...   // assign other fields


    Friday, April 27, 2012 3:23 AM