Understanding Entity Framework: Eager, Lazy, and Explicit Loading

This topic will help us to optimize our loading of related data using Entity Framework Apis.

Lazy Loading

  • Ef core's default behavior

  • Loads data when it's accessed for the first time

  • Does not load child elements if you didn't ask for it

  • Multiple query are executed with child elements

Using a simple foreach loop to retrieve the child elements is inefficient when your goal is just to retrieve all of them.

Instead use Eager Loading

  • Loads all data in one request

  • Uses Include api in EF Core

  • Returns large amounts of data as compared to lazy loading

Here in this example, you can see how clean eager loading and useful when retrieving data with child elements.

Also if the chartOfAccountDetails has a child element EF core has a lot of additional methods you can use like.ThenInclude
Check:Documentation

Explicit Loading

  • Explicitly load navigation property

  • Does not load navigation property on initial load

  • We can load references or collection

Explicit loading can be implemented using the ff:

  • Entry method of the dbContext

  • Reference

  • Collection

What's the difference between Reference and Collection?

Reference can be used on 1:1 relationship and Collection if it's 1: is to many relationship.

But what if we just use IQueryable or IEnumerable and use lambda expressions to filter our data?

Explicit loading is used to load related entities after the initial query has been executed. This is particularly useful when you have already fetched the primary entities and want to load related data conditionally or lazily.

When to Use Each Approach:

  • IQueryable / IEnumerable with Lambda Expressions: Use this when you want to construct dynamic queries that filter or project data before it is retrieved from the database. This approach is ideal when you're certain about the data you need upfront and want to avoid over-fetching.

  • Explicit Loading: Use explicit loading when you want to load related data conditionally, such as when a user enters edit mode or when you need detailed information for only a subset of your data. This allows you to fetch the primary data first and load related data only if necessary, optimizing performance by avoiding unnecessary data retrieval

    Now I'm not a fan of Lazy Loading anymore because of Explicit loading, their difference is just implementing explicit loading coding is more coding while lazy loading let's the EF core do its way. So in short explicit loading has more control and lazy loading can lead up to more executed queries in your DB.
    When to use what:

    1. Use Eager Loading when the relations are not too much. Or you are sure that you want to load all of the data but be wary of the data size, Also when you're guaranteed that you will be using the related entities.

    2. Use Eager Loading when you are sure that you will be using related entities with the main entity everywhere.

    3. Use lazy loading and better explicit loading for better-fined control of fetching data.

That's all for now. I hope you’ve picked up a few valuable insights along the way. Let’s strive to be great engineers, as our world needs more skilled professionals. May all our code be optimized and run smoothly. Cheers!

0
Subscribe to my newsletter

Read articles from Juan Miguel Nieto directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Juan Miguel Nieto
Juan Miguel Nieto

A software developer trying to write organic blogs.