Loading patterns with EF Core
There are three loading patterns that are commonly used with EF: eager loading, lazy loading, and explicit loading. In this section, we're going to introduce each of them.
Eager loading entities
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 used the Include
method to use eager loading (also known as early loading) for the related products:
- Modify the query to comment out the
Include
method call, as shown in the following code:IQueryable<Category> cats = db.Categories; //.Include(c => c.Products);
- In
Main
, comment all methods exceptQueryingCategories
. - Run the console application and view the result, as shown in the following partial output:
Beverages has 0 products. Condiments has 0 products. ...