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

  • Question

  • How do I delete a record from an entity ?

    Below is my code

    public static void DeleteRecord<P>(this DbContext db, P pRecord) where P : class
            {
                try
                {
                    using (var ts = new TransactionScope())
                    {
                        db.Set<P>().Attach(pRecord);  // error here  An entity object cannot be referenced by multiple instances of IEntityChangeTracker.
                         db.Set<P>().Remove(pRecord);                           
                        
                        db.SaveChanges();
                        ts.Complete();
                    }
                }
                catch (SqlException ex)
                {
                    db.Entry<P>(pRecord).Reload();
                    throw new DeleteException(getSQLError(ex.Number));
                }
                catch (Exception ex)
                {
                    db.Entry<P>(pRecord).Reload();
                    throw new DeleteException();
                }

         
            }

    I am having below error on db.Set<P>().Attach(pRecord);

     An entity object cannot be referenced by multiple instances of IEntityChangeTracker.

    Please help

    Tuesday, December 27, 2016 1:23 PM

All replies

  • Hi Sam.A.Dev,

    Based on your description and related code snippet, I create a simple to test the code (code as below), it works fine.

    Could you please provide a bit more information, such as the usage of the method.

    using (var db = new Model1())
    {
        var p = db.Persons.Find(6);
        db.DeleteRecord<Person>(p);
    }

    In addition, here are some similar issue for your referenece.

    http://stackoverflow.com/questions/23326599/avoiding-error-an-entity-object-cannot-be-referenced-by-multiple-instances-of-i

    http://stackoverflow.com/questions/16934376/an-entity-object-cannot-be-referenced-by-multiple-instances-of-ientitychangetra

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 28, 2016 3:24 AM
    Moderator
  • Hi Cole Wu,

    I also posted same thread in asp.net forum please check my thread in below link

    https://forums.asp.net/p/2112693/6106238.aspx?p=True&t=636184788202144833

    Wednesday, December 28, 2016 7:49 AM
  • Hi Sam.A.Dev,

    From the link, I also could not find any usage of this method. i have test the method, it works well, i think that you pass a wrong parameter to method, which causes the issue.

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 28, 2016 2:50 PM
    Moderator

  • I am handing over the application so I am new on it , I will investigate it and will let you know.It could be as you are saying !

    Thursday, December 29, 2016 9:04 AM
  • Hi Sam.A.Dev,

    Do you resolve the issue? if the issue still exists, please feel free let me know.

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 3, 2017 7:00 AM
    Moderator