Tutorials Entity Framework Core Tutorial

Lazy Loading in EF Core

Lazy 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 46 of 100

Lazy Loading in EF Core

Beginner ✓Intermediate ✓AdvancedProfessional

Advanced · 3 — Production skills · ~6 min · Module 5: Relationships

What is this?

Lazy loading automatically loads navigations when accessed on tracked entities — enabled via proxy package and virtual navigations or ILazyLoader injection.

Why should you care?

Legacy ShopNest admin tools may touch order.Customer.Name deep in views — lazy load defers queries until access (use sparingly in APIs).

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).

// Program.cs
options.UseLazyLoadingProxies();

// Entity:
public virtual Customer Customer { get; set; } = null!;

// Usage (tracked entity):
var order = await _context.Orders.FindAsync(10);
Console.WriteLine(order.Customer.Email); // triggers SELECT Customer

What happened?

  • Proxy wraps entity — first access to Customer fires lazy loader query.
  • Requires virtual navigations and context still alive — dangerous in APIs after dispose.

Practice next

  1. Add Microsoft.EntityFrameworkCore.Proxies package.
  2. Mark navigations virtual on entities using lazy load.
  3. Enable UseLazyLoadingProxies in DbContext options.
  4. Disable lazy loading and compare SQL query count in logs for same code path.
  5. Inject ILazyLoader manually instead of virtual proxies.

Remember

Lazy load queries on first navigation access. Needs proxies and alive DbContext. Avoid in Web API JSON serialization.

ShopNest legacy admin refactor

Old WinForms admin relied on lazy load; new API replaces with explicit Include.

Outcome: N+1 eliminated — product listing API response time drops 80%.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Junior PDF Detailed
What is lazy loading?
Short answer: re proxies? Lazy loading delays the loading of related data until it's accessed for the first time. EF Core requires proxies for lazy loading: Install NuGet: Microsoft.EntityFrameworkCore.Proxies Enable in…
Junior PDF Detailed
What is lazy loading?
Short answer: How to enable lazy loading in EF Core? What are proxies? Lazy loading delays the loading of related data until it's accessed for the first time. EF Core requires proxies for lazy loading: Install NuGet: Mic…
Mid PDF Detailed
What performance implications are there for eager vs lazy loading?
Short answer: Loading Type Pros Cons Eager Fewer queries, good for large data sets Loads everything even if not used Lazy Loads only when needed Risk of N+1 queries, more round-trips Explicit Fine-grained control More co…
Mid PDF Detailed
Can you disable lazy loading?
Short answer: How? Yes. You can disable lazy loading globally: options.UseLazyLoadingProxies(false); Or don't install the proxy package at all. You can also disable it for specific navigation properties by not making the…
Junior PDF Detailed
What is eager loading?
Short answer: Eager loading loads related data as part of the initial query, reducing round-trips to the database. ✅ Use .Include() to load related entities: var blogs = context.Blogs .Include(b => b.Posts) .ToList();…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

Entity Framework Core Tutorial
Course syllabus

Entity Framework Core Tutorial

Module 1: EF Core Fundamentals
Module 2: Code First Approach
Module 3: CRUD Operations
Module 4: LINQ
Module 5: Relationships
Module 6: Advanced EF Core
Module 7: Performance Optimization
Module 8: Enterprise Architecture
Module 9: Testing & Debugging
Module 10: Real-World Projects
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details