Loading Patterns in EF Core
Explore the three main EF Core loading patterns: eager loading, which loads related data early; lazy loading, which loads data automatically as needed; and explicit loading, which allows manual control over data loading. Understand their implementation and trade-offs to handle related data efficiently in your projects.
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 ...