Explicit loading entities

Another type of loading is explicit loading. It works like lazy loading, but you are in control of exactly which related data is loaded and when. You can think of lazy loading as explicit loading that happens automatically for all related entities.

In the QueryingCategories method, modify your query statements to prompt the user if they want to enable eager loading and explicit loading, as shown in the following code:

IQueryable<Category> cats; // = db.Categories;//.Include(c => c.Products); Write("Enable eager loading? (Y/N): "); bool eagerloading = (ReadKey().Key == ConsoleKey.Y); bool explicitloading = false; WriteLine(); if (eagerloading) { cats = db.Categories.Include(c => c.Products); } else { cats = db.Categories; ...

Get C# 7.1 and .NET Core 2.0 – Modern Cross-Platform Development - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.