Entity Framework is not loading the reference and collection properties lazily

c# entity-framework entity-framework-6 lazy-loading


I have Backpack and Book entities. Book references Backpack (one to many). I am creating an instance of Backpack and bunch of Books. So in this case backpack has bunch of books. I am saving those entities to the db. I am verifying that those got saved to the db. When I try to load backpack it loads fine and all the properties are set except the navigation properties. I am also checking that LazyLoading is not disabled. My navigation properties has the virtual keyword. I am not sure what I am doing wrong. If I try to Load the backpack with Include() it loads the books:


I am trying to figure out why it is not loading the books lazily? I have the same problem with loading the book. When I load the book, it doesn't have the backpack attached. I see that the BackpackId is there.

In my property getter/setter I have some logic that will be fired, but I am not sure how that could be a problem.

4/19/2017 9:32:17 PM

Accepted Answer

After a several days of debugging, finally figured out the problem. As people mentioned above, you have to enable the LazyLoading and ProxyCreating. I had the issues even after having the enabling the LazyLoading and ProxyCreating. Also make sure you declare your navigation properties as virtual, otherwise EF will not be able to load entities lazily.

So the issue I had was, EF wasn't crating Proxies because of my entity didn't have a public or protected constructor with NO parameters. After creating public (in my case protected) constructor without parameter it worked.

NOTE: Not having public/protected constructor without parameters will not affect the eager loading.

Here is a link that explains the requirements for the LazyLoading

12/13/2017 3:16:31 AM

Popular Answer

With the limited information at hand, I can see the following explanations for your problem.

Lazy Loading or Proxy Creation are disabled

Make sure that lazy loading and proxy creation are enabled, the first doesn't work without the latter.

dbContext.Configuration.ProxyCreationEnabled = true;
dbContext.Configuration.LazyLoadingEnabled = true;

(see this SO post for details)

Accessing the entities after disposing the context

Simplified, Lazy Loading works like this:

  • Whenever you retrieve an entity from the context, you actually get an object of an automatically created subclass of the class you expected that overrides your virtual navigation properties, which is the proxy.
  • Whenever you then access said navigation properties, the proxy will access the database and load the linked entities when needed.

This last step is, of course, only possible if the entity/proxy is still attached to the context and can therefore query the database to retrieve said objects.

using( var dbContext = new MyContext() )
    dbContext.Configuration.ProxyCreationEnabled = true;
    dbContext.Configuration.LazyLoadingEnabled = true;
    // Note: You can modify or derive from `MyContext` so that this is
    //       done automatically whenever a new `MyContext` is instantiated

    var backpack = dbContext.Backpacks.Where(b=>b.Name!="").FirstOrDefault();

    // This should work
    var firstBook = backpack.Books.FirstOrDefault();

// This will probably not, because the context was already disposed
var firstDrink = backpack.Drinks.FirstOrDefault();

I hope this helps, but feel free to provide more information if it doesn't

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow