Loading Patterns in EF Core

Learn about common loading patterns in EF Core i.e. eager loading, lazy loading, and explicit loading.

Three loading patterns are commonly used with EF Core:

  • Eager loading: Load data early.

  • Lazy loading: Load data automatically just before it is needed.

  • Explicit loading: Load data manually.

In this section, we’re going to introduce each of them.

Eager loading entities using the Include extension method

In the QueryingCategories method, the code currently uses the Categories property to loop through each category, outputting the category name and the number of products in that category. This works because when we wrote the query, we enabled eager loading by calling the Include method for the related products. Let’s see what happens if we do not call Include:

Step 1: Modify the query to comment out the Include method call, as shown in the following code:

Press + to interact
IQueryable<Category>? categories = db.Categories;
//.Include(c => c.Products);
...