views:

16

answers:

0

Hi,

I'm working in a disconnected scenario, but I noticed that disposing an object context does not release attached entities. As a result, subsequent operations often fail because of this.

So to solve this, I detach everything myself when the object context is being disposed:

public void Dispose()
{
   // detaching is not really needed, because we have short living object contexts
   var objectStateEntries = 
       _context.UnderlyingContext.ObjectStateManager.GetObjectStateEntries(EntityState.Unchanged);           
   objectStateEntries.ToList().ForEach(o => { if (o.Entity != null)
   {                
     _context.UnderlyingContext.Detach(o.Entity);               
   }});            

   _context.Dispose();
   _context = null;    
}

However, the side effect is that the object graph gets detached completely, but I really want to keep the graph!

It seems I don't find a solution for this, is it true that it can't be done?