title: EF Code First Lazy loading Not Working
body: <p>I am using code first with EF6 but cannot seem to get lazy loading to work. Eager loading is working fine. I have the following classes:</p>
  
  <pre><code>public class Merchant : User
  {
      ...
  
      public virtual ICollection&lt;MerchantLocation&gt; MerchantLocations { get; set; }
  }
  
  public class MerchantLocation : BaseEntity
  {
      ...
  
      public int MerchantId { get; set; }
      public virtual Merchant Merchant { get; set; }       
  }
  
  public class User : BaseEntity
  {
      ...
  }
  
  public class BaseEntity
  {
      ...
  
      public int Id { get; set; }
  }
  </code></pre>
  
  <p>I test my lazy loading of the locations via the following code (which fails):</p>
  
  <pre><code>public void Test_Lazy_Loading() {
      using (var context = new MyDbContext()) {
          var merchant = context.Users.OfType&lt;Merchant&gt;.First();
          merchant.MerchantLocations.ShouldNotBeNull(); // fails
      }
  }
  </code></pre>
  
  <p>However eager loading works fine:</p>
  
  <pre><code>public void Test_Eager_Loading() {
      using (var context = new MyDbContext()) {
          var merchant = context.Users.OfType&lt;Merchant&gt;.Include("MerchantLocations").First();
          merchant.MerchantLocations.ShouldNotBeNull(); // passes
      }
  }
  </code></pre>
  
  <p><code>MerchantLocations</code> is marked as <code>public virtual</code> so I'm not sure what the problem is. I have also added the following in my <code>DbContext</code> constructor:</p>
  
  <pre><code>Configuration.LazyLoadingEnabled = true;
  Configuration.ProxyCreationEnabled = true;
  </code></pre>
  
  <p>edit: I have also noticed that the <code>merchant</code> object being returned in the above tests is not an EF proxy. It is a plain <code>Merchant</code>. I suspect that this is causing the problem.</p>