Note that there are some explanatory texts on larger screens.

plurals
  1. POEF: load references after the context was disposed?
    text
    copied!<p>I'm struggling with the ADO.NET Entity Framework. This is my ER-diagram:</p> <pre><code>--------------------- ----------------- --------------- | GrandParentEntity |&lt;-------| ParentEntity |&lt;-------| ChildEntity | +-------------------+ 1 N +---------------+ 1 N +-------------+ | Id | | Id | | Id | --------------------- | GrandParentFk | | ParentFk | ----------------- --------------- </code></pre> <p>Have a question about the lifetime of an object context. Assume that i requests my data as follows:</p> <pre><code>public static List&lt;MyParentEntity&gt; GetMyParentEntity() { using (var context = new MyObjectContext(blablabla...)) { var resultSet = from e in context.MyParentEntitySet select e; return resultSet.ToList(); } } </code></pre> <p>I get a list of all my parent entities. Some time later (hours?), the user decides which one he would like to inspect. (Meanwhile, the entity object passed through several tiers of the application.) I now have to load all the details of the record:</p> <pre><code>public static void LoadChildren(MyParentEntity pEntity) { using (var context = new MyObjectContext(blablabla...)) { pEntity.GranParentEntityReference.Load(); pEntity.ChildEntites.Load(); } } </code></pre> <p>This leads to an ObjectDisposedException, because the MyParentEntity object has lost its connection to the object context that created it. I have several possibilities to handle this:</p> <p>1) I can use one and the same instance of MyObjectContext all the time and never close it. This leads to a huge waste of memory.</p> <p>2) I can Detach() manually each and every single entity object (and its child and parent entities) every time I leave the "using (context)" block. And I can Attach() them every time i enter a new "using(context)" block. Leads to a lot of effort. (I my opinion a framework should reduce the effort instead of increase it.)</p> <p>3) Reload and then throw away each entity every time I enter a new "using(context)" block. Leads to more (unnecessary) load on the SQL Server and also wastes memory.</p> <p>4) I can load all details and all references and all references of all references and the references of those references when the application starts. (No discussion, this really is silly!)</p> <p>5) ... (Did I forget an option?)</p> <p>Now the big QUESTION for you: which way should i choose? Is there another way that i didn't see? Or did i misunderstand the spirit of the framework entirely?</p> <p>Thanks in advance</p> <p>UPDATE: It is a Windows Forms Application.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload