Object structure: delete, but save the object graph

I work in a disconnected script, but I noticed that deleting the context of an object does not release attached objects. As a result, subsequent operations often fail.

So, to solve this problem, I myself disable everything when the object context is placed:

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, a side effect is that the graph of the object is completely separated, but I really want to keep the graph!

It seems I have not found a solution for this, is it true that this is impossible?

+3
source share
1 answer

; , EF. ? .

:

myDataContext.MyEntitySet.MergeOption = MergeOption.NoTracking;
0

Source: https://habr.com/ru/post/1770605/


All Articles