none
Complex objects cannot be referenced multiple times by the same entity RRS feed

  • Question

  • I am using EF 4.2/4.1 Code First, and I have a POCO entity (Customer) that contains a complex type "Address".

    When I try to update Customer using DbContext:

    public class SCMDBContext:DbContext
    {
        public DbSet<Customer> Customers { get; set; }
    }
    
            var cust = context.Customers.Find(1);
            cust.Code = "ALFKI";
            context.SaveChanges();

    I get the following error:

    System.InvalidOperationException was unhandled by user code
      Message=The entity of type 'System.Data.Entity.DynamicProxies.Customer_083A61E5C69F90F6DFD91F2ECB5B2469C0FC316DFC637543C1B4DE3446F95297' references the same complex object of type 'SCM.BL.Entity.Address' more than once. Complex objects cannot be referenced multiple times by the same entity.
      Source=System.Data.Entity
      StackTrace:
           at System.Data.Objects.EntityEntry.CheckForDuplicateComplexObjects(Object complexObject)
           at System.Data.Objects.EntityEntry.DetectChangesInProperties(Boolean detectOnlyComplexProperties)
           at System.Data.Objects.ObjectStateManager.DetectChangesInScalarAndComplexProperties(IList`1 entries)

    Can someone help?

    Thanks,

    Rohit


    C# Developer
    • Edited by Rohit Gupta Friday, November 25, 2011 6:47 AM i get error during update of Customer Entity, not during add
    Thursday, November 24, 2011 2:05 PM

Answers

  • There was an issue with the Equals implementation which caused this error. The property Customer.Code being changed was part of the Equals check hence it caused two different entities being tracked by the DbContext. I corrected the Equals implementation then this error was resolved.
    C# Developer
    Friday, November 25, 2011 9:37 AM

All replies

  • There was an issue with the Equals implementation which caused this error. The property Customer.Code being changed was part of the Equals check hence it caused two different entities being tracked by the DbContext. I corrected the Equals implementation then this error was resolved.
    C# Developer
    Friday, November 25, 2011 9:37 AM
  • Hi Rohit,

    Thanks for sharing your experience here.

    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.

    Monday, November 28, 2011 8:40 AM
    Moderator