Loading and tracking patterns with EF Core
There are three loading patterns that 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
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
Program.Queries.cs, in the
QueryingCategoriesmethod, modify the query to comment out the
Includemethod call, as shown highlighted in the following code:
IQueryable<Category>? categories = db.Categories;
//.Include(c => c.Products);