title: How do I use Entity Framework association "fixup"?
body: <p>I need to retrieve a large hierarchy of entities from my database, with many relationships. Rather than create one giant queries with many <code>Includes</code>, I've read that it's possible to use a number of smaller queries to fetch different parts of the hierarchy, then EF somehow glues everything together using something called "association fixup". However I'm struggling to get it to work. (I'm using EF5 and POCO by the way).</p>
  
  <p>As a simple example, I'm trying to retrieve a Customer and all their related Orders using the "fixup" technique. This is what I'm doing in my BLL tier:-</p>
  
  <pre><code>var customer = context.Customers
      .Where(o =&gt; o.Id == requestedCustomerId).SingleOrDefault();
  
  customer.Orders = context.Orders
      .Where(o =&gt; o.CustomerId = requestedCustomerId).ToList();
  </code></pre>
  
  <p>When I examine the customer entity returned to the UI tier, customer.Orders gives an ObjectDisposedException. What am I doing wrong?</p>
  
  <p>As a further example of how fixup would be used, how would I populate the Orders' related OrderLines (either in a separate query, or as part of the second query above)? And what if the OrderLine entity had a ProductCategory parent entity - how would I populate these?</p>
  
  <p><strong>Update</strong></p>
  
  <p>I've just tried the following, which works:-</p>
  
  <pre><code>var orders = context.Orders
      .Where(o =&gt; o.CustomerId = requestedCustomerId).ToList();
  
  var customer = context.Customers
      .Where(o =&gt; o.Id == requestedCustomerId)
      .Include("Orders")
      .SingleOrDefault();
  </code></pre>
  
  <p>Am I right in saying that the second query won't fetch Orders from the database again, and EF will associate those retrieved by the previous query (even though they are just sitting in some arbitrary variable)?</p>