Mid-levelMid (3–6 yrs)C#.NETMicrosoftAmazonUber
N+1 queries in EF Core
How do you detect and fix N+1 query problems in EF Core?
Answers use simple, clear English.
Quick interview answer
N+1 happens when you load a list then lazily access navigation properties per item. Fix with Include/ThenInclude, AsSplitQuery for large graphs, projection to DTOs with Select, or explicit Join. Detect with logging, MiniProfiler, or Application Insights SQL dependency traces.
Detailed answer
N+1 happens when you load a list then lazily access navigation properties per item. Fix with Include/ThenInclude, AsSplitQuery for large graphs, projection to DTOs with Select, or explicit Join. Detect with logging, MiniProfiler, or Application Insights SQL dependency traces.
Code example
var orders = await db.Orders
.AsNoTracking()
.Include(o => o.Items)
.Where(o => o.CustomerId == id)
.Select(o => new OrderDto(o.Id, o.Items.Count))
.ToListAsync(ct);midseniorfull-stack#efcore#performance#sql