title: Lazy loading of related entities not working with Entity Framework 4.1
body: <p>We have a project using Entity Framework 4.1 Code First for data storage. It was coded in such a way that EF is abstracted behind a repository pattern. When entities are fetched from a repository, the dataset is passed a huge list of include strings telling it all the related entities that should also be fetched. This results in an insanely huge query which can take 10 seconds to run - even when there is no data in the database!</p>
  
  <p>There is already a lot of code written using the repository to access the database, I need to improve the performance ideally without breaking the pattern.</p>
  
  <p>I am trying to change things so that when searching the database, no include strings are passed to the dataset (or just minimal include strings). When entities are fetched from the database, their related entities will be loaded through lazy loading when they are first accessed.</p>
  
  <p>I have a class called Donation with a navigational property like this:</p>
  
  <pre><code>public virtual Employee Donor { get; private set; }
  </code></pre>
  
  <p>Both Donation and Employee classes have a private parameterless constructor. When using the include string "Donor", I can fetch a list of Donations and they are returned with Donor attached.</p>
  
  <p>When I fetch a Donation from the repository without the include string, Donor is usually returned as null. If co-incidentally I am logged in as the Employee in question, another query elsewhere in the system will have already fetched my Employee entity from the Employee repository. When this happens, Donation is returned with Employee set correctly! So clearly the data is being "lazy loaded" if it is already available from an earlier query. But in general I won't have already queried for the Donor and all the other entities.</p>
  
  <p>Can anyone suggest what I could be doing wrong? Sorry for the long question. I can post more bits of code if needed, but it is quite disparate. Thanks in advance!</p>