title: EntityFramework Not Loading Related Data
body: <p>I have these entities:</p>
  
  <pre><code>public class Company : PrimaryKey
  {
      public string Name {get;set;}
  
      public virtual Account Account {get;set;}
  }
  
  public class Account
  {
      [Key]
      public Guid CompanyId { get; set; }
  
      public virtual Company Company {get;set;}
  }
  </code></pre>
  
  <p>I use these configurations:</p>
  
  <pre><code>modelBuilder.Entity&lt;Company&gt;()
      .HasOptional(c =&gt; c.Account)
      .WithRequired(a =&gt; a.Company)
      .WillCascadeOnDelete();
  </code></pre>
  
  <p>Now, I have two projects, one is a test bench project which is a <code>Console Application</code> with a <code>DbContext</code> and a <code>Repository</code>, the second is the full blown production project which is a <code>MVC 4</code> in which I use <code>Dependancy Injection</code> to create a Repository  <code>.InTransientScope()</code> which in turn loads a new context each time it is called.</p>
  
  <p>Both have exactly the same contexts and repositories (the product obviously has Interfaces).</p>
  
  <p>in the test bench when I call this:</p>
  
  <pre><code>_repository.GetById&lt;Company&gt;(id);
  </code></pre>
  
  <p>All of it properties are filled out, i.e. eager loading</p>
  
  <p>in the production when I call the same line, nothing is loaded and its not loaded till I created another function which does this:</p>
  
  <pre><code>_dbContext.Companies.Include("Account").FirstOrDefault(x =&gt; x.Id.Equals(id));
  </code></pre>
  
  <p>Of which, when executed does provide all the <code>Account</code> information, but funnily bar any other navigation properties that <code>Account</code> contains!!! Even though I have disable LazyLoading, it still doesn't work. </p>
  
  <p>This is surprising because both projects are fundamentally the same, bar the use of the IoC DI in one of them....</p>
  
  <p>Why is this happening? How can I specify in a predominantly generic Repository to eager load all this information at the <code>Controllers</code> preference....?</p>
  
  <p><em><strong>Break Points</em></strong></p>
  
  <p>I set break points in both projects too look at the ADO.NET call to the database and the sql statement that was executed, in the test bench it did go off and call the information, in the production it did not show any joins or anything of that nature what so ever.</p>
  
  <p><em><strong>Other Things Tried</em></strong></p>
  
  <p>I tried accessing the navigation property directly when loading it from the database:</p>
  
  <pre><code>var acc = _repository.GetById&lt;Company&gt;(id).Account;
  </code></pre>
  
  <p>It still says <code>null</code>. So my repository/context is not even loading any related data when asked for it.... what is going on?!</p>
  
  <p><em><strong>Definitions</em></strong></p>
  
  <pre><code>_repository.GetById&lt;Company&gt;(id);
  </code></pre>
  
  <p>is:</p>
  
  <pre><code>public T GetById&lt;T&gt;(Guid id)
  {
      return _dbContext.Set&lt;T&gt;().FirstOrDefault(x =&gt; x.Id.Equals(id));
  }
  </code></pre>