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.

View all posts by Ryan