Tutorials Entity Framework Core Tutorial
Explicit Loading in EF Core
Explicit Loading in EF Core: free step-by-step lesson with examples, common mistakes, and interview tips — part of Entity Framework Core Tutorial on Toolliyo Academy.
On this page
Entity Framework Core Tutorial · Lesson 47 of 100
Explicit Loading in EF Core
Beginner ✓ → Intermediate ✓ → Advanced → Professional
Advanced · 3 — Production skills · ~6 min · Module 5: Relationships
What is this?
Explicit loading fetches related data on demand with Entry(entity).Reference().LoadAsync() or Collection().LoadAsync() after the principal is already loaded.
Why should you care?
ShopNest order detail API loads Order first, then explicitly loads Items only if user expands line items — saves bandwidth on summary view.
See it live — copy this example
Paste into a .NET project with EF Core packages, then run with LocalDB/SQL Server (dotnet ef / dotnet run).
var order = await _context.Orders
.AsNoTracking()
.FirstOrDefaultAsync(o => o.Id == orderId);
if (order is null) return NotFound();
await _context.Entry(order).Collection(o => o.Items).LoadAsync();
await _context.Entry(order).Reference(o => o.Customer).LoadAsync();
What happened?
- First query loads Order only.
- LoadAsync on Collection and Reference issues separate SELECTs for Items and Customer when you choose.
Practice next
- Load principal with AsNoTracking if read-only.
- Attach entity if detached before Entry().LoadAsync.
- Use Query() on collection for filtered explicit load.
- Use Entry(order).Collection(o => o.Items).Query().Where(i => i.Quantity > 1).LoadAsync().
- Load reference only when query param ?includeCustomer=true.
Remember
Explicit load uses Entry().LoadAsync. Good for conditional related data. Entity must be tracked or attached.
ShopNest order expand
Mobile order list loads headers; detail screen explicitly loads Items collection.
Outcome: List view stays fast; detail view pays cost only when opened.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!