Enable or disable LazyLoading in Entity Framework

Abilitare e disabilitare il Lazy Loading con Entity Framework

One of the most interesting Entity Framework features is the Lazy Load function, which allows a deferred data load of your related entities, meaning that DB data won't be loaded until you specifically request it.

To better understand how it's working, let's take these sample Entity classes:

We can notice that the StudentAddress property of the Student class is referring to another Entity (StudentAddress), which is also related to a specific DB table. If we request a list of Students with LazyLoading enabled, the data provider will get all of our students from the DB but each StudentAddress property won't be loaded until the property will be explicitly accessed.

 

Disable Lazy Loading

In Entity Framework 4 and beyond Lazy Loading is enabled by default. We can disable it globally, on DbContext level, or selectively, on per-property level.

To disable it globally you need to set the DbContext's LazyLoadingEnabled property to false in the object's constructor:

To disable it on per-property level, all you need to do is to make the property non-virtual. For example, we could modify the Student class in the following way:

IMPORTANT: You could easily think that, once you disable Lazy Loading, the framework will auto-load each and every related property: it won't. Don't worry, it's a good thing! You don't want your DB to be automatically wasted on each Entity query request. On the opposite, disabling Lazy Loading means that you will have to manually handle each related property loading. Let's see how you can do that.

 

Manually loading data

You can manually control which property to load in two different ways: during an Entity Request (Eager Loading) or with a single Property Load.

 

Property Loading

If you choose Property Loading, all you need to do is to use the Load() method supplied by the entity framework:

The method can ben used to load either one-to-one and one-to-many relationships. For example, we could request the loading of the Teachers collection property in the following way:

 

Eager Loading

The Eager Loading function is useful when you want to load the main Entity (or Entity collection) togheter with its (theirs) related entities right from the start, possibly using a single query command. In order to use that you need to use the Include() method in the following way:

IMPORTANT: If you can't find the Include() method, check that you added the System.Data.Entity namespace.

You can also use Eager Loading to load nested, multi-level properties. For example, we could load the StudentAddressDetails collection property of each StudentAddress item (cfr. the StudentAddress class definition above) in the following way:

And so on.

That's it for now: happy coding!

 

About Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies. Microsoft MVP for Development Technologies since 2018.

View all posts by Ryan

8 Comments on “Enable or disable LazyLoading in Entity Framework”

  1. Pingback: How to specify what level of SQL hierarchy to return? | Solutions for enthusiast and professional programmers
  2. Hi Ryan, thanks for an awesome explanation and nice and easy to understand examples. Could you please add vk.com share button to your website? Cheers

  3. Hi Ryan!
    Thanks, article great!
    I have proplem this. Lazy load init all object when Application running first and it very slow. How to resolve for proplem this?

    1. Hello, Lazy Load won’t init all objects when the Application starts: it actually does quite the opposite, i.e. load items as they’re needed.

      Of course if you have a huge home page with a lot of nested contents, Lazy Load won’t be of any use because you *will* need them all when you load it the first time. If that’s the case, you should probably optimize your EF relationships and/or LINQ/Lambda expressions to retrieve DB data more efficiently, which is something you can often do better with (selective) Eager Loading.

      You should run a benchmark tool to measure how much time you need for each EF call and then pinpoint the slow query(es). If you want, I can post a nice helper class I’m using to do just that: it’s basically a time tracker where you can define multiple “laps” and show them all at the end of the response, i.e. inside a view.

      Please let me know.

      1. Dear @Dark, I happy, when you can share it with me.
        Real, when i use EF, first application running is very slow.
        Thanks Dark!

        1. Here it is: I wrote the post just for you :)

          https://www.ryadel.com/en/timetracker-c-sharp-class-to-measure-source-code-execution-time/

          Try to use it to find your performance bottleneck.

  4. Hey,
    If you define your navigation property virtual, Entity Framework will at runtime create a new class (dynamic proxy) derived from your class and uses it instead of your original class. This new dynamically created class contains logic to load the navigation property when accessed for the first time. This is referred to as “lazy loading”. It enables Entity Framework to avoid loading an entire tree of dependent objects which are not needed from the database.

    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.