I have a "Entity object cannot reference multiple instances of the IEntityChangeTracker problem." After some checking around, it seems like I have an object that is being tracked for changes. The problem is that I don’t know the source of the problematic object ... it was clearly placed in context, but I'm not sure which call was not properly disconnected.
So, after several hours of trying to figure it out, I’m looking for how to walk through the tree to find the source object that I came across, because maybe this will help me understand where the source object is added.
The error is raised on line 226, so it looks like I have a “stealth” client, or perhaps one of the properties of the Client causes this, because the Client has a few more properties that are their own complex object types ...
Line 224: if (null != this.Customer)
Line 225: {
Line 226: context.Entry(this.Customer).State = EntityState.Unchanged;
Line 227: }
The error does not say which object is causing the error, it simply points to line 226. After assuming the phantom Customer object that causes this, I tried:
var test = ((IObjectContextAdapter)dataContext).ObjectContext.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Deleted | EntityState.Modified | EntityState.Unchanged);
foreach(var e in test)
{
if(e.GetType() == typeof(Customer))
{
dataContext.Detach(e);
}
}
The idea was to scroll through an object containing links to all objects, we hope to find a naughty Client and give it a download. But alas, that did not work; There are no clients in this cycle. Oh, FYI - this runs a few lines before the previous code, so I do not creep into the creation of any additional object.
So, I need a way to determine which object is actually causing the error.
@Ladislav - FYI - , - (BO). - Windows, - .. , hugo. BO Save(). current saveUpdate:
public void SaveOrUpdate(DataContext context)
{
if (context.Entry(this).State == EntityState.Detached)
{
context.Customers.Add(this);
context.SaveChanges();
}
else
{
context.Entry(this).State = System.Data.EntityState.Modified;
context.SaveChanges();
}
}
, : , , DataContext, . , , .. , int string.
concurrency, , DataContext - , . --, . , Customer BO DataContext, DataContext Disposed, DataContext, - , BO BO
, StackOverflow ( , ), LifeContext Lifetime Management 8 Entity Framework Gotchas Julia Lerman
, , Dispose, , , DataContext .
, "" DataContext, DataContext , , , . . , Linq to SQL -, , .
, DataContext, , , , DataContext .
, , , , , . , BOs :
MessageGroup
- :
- :
- :
- :
- :
- : MessageGroup
- :
- :
- :
, MessageGroup ( Xml), . MessageGroup Injection Dependency Injection DataContext - BOs DataContext. ( ) MessageGroup... - , - DataContext.
MessageGroup.Messages , BO - User. Customer ( MessageGroup) . , context.Users.Add(this), . , .
, ( , ), , , . , , - :
var cust = Customer.GetCustomerFromExternalId(crm.CustomerId);
dataContext.Detach(cust);
dataContext.SaveChanges();
, . , , , ?
, . , EF CodeFirst ? , SubSonic NHibernate?