Loading patterns with EF Core
There are three loading patterns that are commonly used with EF: lazy loading, eager 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
, as shown in the following code:static void Main(string[] args) { QueryingCategories(); // QueryingProducts(); // QueryingWithLike(); }
- Run the console application and...