I would like to clear the ObjectStateManager, so that after calling SaveChanges();
on the DbContext the following line returns no result:
dbContext.ObjectContext.ObjectStateManager.GetObjectStateEntries(EntityState.Unchanged);
The behaviour seems to be that all objects (Added, Modified) in the ObjectStateManager gets the State changed to Unchanged
, so the code will return them all. Is there a way to clear it?
I need this since I am reusing the context and do some stuff for the Entities with Unchanged state but since the ObjectSateManager grows and grows with Unchanged Entities (since it changes them all to Unchanged after SaveChanges) its doing the same work for one Entity over and over.
Edit:
Why the detach method isnt working for me:
Lets assume you have 2 classes:
public class Nation
{
public string Name { get; set; }
public ICollection<City> Cities { get; set; }
}
public class City
{
public string Name { get; set; }
public Nation Nation { get; set; }
}
Now I have passed to the SaveChanges a Nation Item with some Cities that need to be either updated or inserted.
Lets assume the following:
var canada = new Nation()
{
Name = "Canada",
}
canada.Cities = new City[]
{
new City(){ Name = "Ottawa", Nation = canada, },
new City(){ Name = "Edmonton", Nation = canada, },
new City(){ Name = "Victoria", Nation = canada, },
new City(){ Name = "Torronto", Nation = canada, }
},
}
Now I have all those objects in the Unchanged state inside my ObjectStateManager. (After the SaveChanges call)
I than loop through them and set the state to unchanged, this result in every City having Nation = null
and Nation.Cities being empty
.
I found a solution that works for me "perfectly", it doesnt Clear the ObjectStateManger but ignores all the previously attached/processed entities.