title: Entity framework: detaching but keeping object graph
body: <p>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.</p>
  
  <p>So to solve this, I detach everything myself when the object context is being disposed:</p>
  
  <pre><code>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 =&gt; { if (o.Entity != null)
     {                
       _context.UnderlyingContext.Detach(o.Entity);               
     }});            
  
     _context.Dispose();
     _context = null;    
  }
  </code></pre>
  
  <p>However, the side effect is that the object graph gets detached completely, but I really want to keep the graph!</p>
  
  <p>It seems I don't find a solution for this, is it true that it can't be done?</p>