I had a bit of a struggle trying to understand why my code was crashing (which I got to work).
When you look at both the original method and the working one, the placement of one line is different
ctx.Inventories.Attach(this);
I’m puzzled when the original method doesn’t work but the second one does.
Can anyone provide some insight?
Here’s the exception that I get.
System.InvalidOperationException : An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.
This is my original method
public void RemoveDependency(int depId)
{
bool returnValue = false;
if (this.Id != 0 && depId > 0)
{
using (ApsEntities ctx = new ApsEntities())
{
var query2 = from d in ctx.Dependencies
where d.Id == depId
select d;
Dependency found = query2.FirstOrDefault();
if (found != null)
{
**ctx.Inventories.Attach(this);**
ctx.ObjectStateManager.ChangeObjectState(this, EntityState.Modified);
this.Dependencies.Remove(found);
ctx.SaveChanges();
}
}
}
return returnValue;
}
Here is my working method
public void RemoveDependency(int depId)
{
bool returnValue = false;
if (this.Id != 0 && depId > 0)
{
using (ApsEntities ctx = new ApsEntities())
{
**ctx.Inventories.Attach(this);**
var query2 = from d in ctx.Dependencies
where d.Id == depId
select d;
Dependency found = query2.FirstOrDefault();
if (found != null)
{
ctx.ObjectStateManager.ChangeObjectState(this, EntityState.Modified);
this.Dependencies.Remove(found);
ctx.SaveChanges();
}
}
}
return returnValue;
}
The behavior has to do with the
ObjectStateManagertracking the relationships.When the current
Inventoryitem is not known to theObjectContext(as in your first example) then the relationships betweenInventoryandDependencyis not recognized by the context. SinceInventoryis unknown to theObjectContextyour query for aInventorywill load it and the item already exists when you attach it to yourObjectContext.In the second example you first attach
Inventoryand then execute the query. TheObjectContextwill then explicitly attach theDependencyobject to theInventoryitem.The documentation states:
This behavior can’t happen when the related objects are not known to the
ObjectContext.