Seven EF Core habits that quietly slow down production
Short answer: EF Core gets slow when it is asked to load more data, in more round trips, than the page needs. The usual suspects are N+1 queries, tracking on read-only queries, loading full entities instead of projecting, cartesian explosion from several Includes, filtering in memory, Count instead of Any, and OFFSET paging or per-row updates on big tables. Each has a one- or two-line fix.
A real example: 301 queries for one list
A "my policies" page took four seconds. The SQL log showed one query for the policies and then one query per policy for the holder, and another for the latest payment: 301 queries for a list of 150 rows. Each query was fast. Together they were not.
Replacing the loop with a single projection brought the page to 120 ms. No index, no cache, no Dapper. Just one query instead of 301.
Step zero: see the SQL
// Development only: print every SQL statement EF Core sends
builder.Services.AddDbContext<AppDbContext>(o => o
.UseSqlServer(builder.Configuration.GetConnectionString("Default"))
.LogTo(Console.WriteLine, LogLevel.Information));
// Or for a single query:
var sql = db.Policies.Where(p => p.Status == PolicyStatus.Active).ToQueryString();
You can't fix what you can't see. Read the generated SQL for every screen you suspect, count the round trips, and look at the execution plan for the expensive ones.
1. N+1 queries and 2. full entities
// N+1: one query for the policies, then one more per policy
var policies = await db.Policies.Where(p => p.Status == PolicyStatus.Active).ToListAsync(ct);
foreach (var p in policies)
{
var holder = await db.Holders.FindAsync([p.HolderId], ct);
rows.Add(new PolicyRow(p.Number, holder!.FullName, p.PremiumRials));
}
// One query, only the columns the screen needs, no tracking
var rows = await db.Policies
.Where(p => p.Status == PolicyStatus.Active)
.Select(p => new PolicyRow(p.Number, p.Holder.FullName, p.PremiumRials))
.ToListAsync(ct);
Projection with Select fixes both problems at once: one round trip, and only the columns the page shows. Projections aren't tracked, so they are cheaper too.
3. Cartesian explosion
Two collection Includes in one query return every combination of rows. An order with 20 items and 10 payments sends 200 rows for 30 facts.
var order = await db.Orders
.Include(o => o.Items)
.Include(o => o.Payments)
.AsSplitQuery() // three simple queries instead of one Items × Payments product
.SingleAsync(o => o.Id == id, ct);
4 to 7. The rest
// Read-only queries: skip change tracking
var list = await db.Policies.AsNoTracking().Where(...).ToListAsync(ct);
// Existence: Any, not Count
var hasPaid = await db.Payments.AnyAsync(p => p.UserId == userId, ct);
// Filter in SQL, not in memory
var bad = (await db.Payments.ToListAsync(ct)).Where(p => p.UserId == userId); // loads the table
var good = await db.Payments.Where(p => p.UserId == userId).ToListAsync(ct);
// Keyset paging for long lists: no OFFSET scan
var page = await db.LedgerEntries
.Where(e => e.AccountId == accountId && e.Id < lastSeenId)
.OrderByDescending(e => e.Id)
.Take(50)
.ToListAsync(ct);
// Bulk changes without loading entities (EF Core 7+)
await db.OutboxMessages
.Where(m => m.ProcessedAtUtc < cutoff)
.ExecuteDeleteAsync(ct);
Pros and cons of fixing these
Pros
- Most fixes are small and local, and the gains are often 10× or more.
- Fewer round trips also means less load on SQL Server for everyone else.
- The code gets more explicit about what each screen actually needs.
Cons
- Projections create small DTO types you have to maintain.
- Split queries are not a consistent snapshot unless you use a transaction with suitable isolation.
- Keyset paging can't jump to "page 37"; it suits feeds and long lists, not every UI.
An EF Core application that gets slower every month? See how I can help.
Frequently asked questions
Should I switch to Dapper when EF Core is slow?
Usually not first. Most slow EF Core code is fixed by projection, no-tracking queries and removing N+1 patterns. For the few hot, complex read queries that remain, a targeted Dapper query next to EF Core is a reasonable choice.
Is lazy loading always bad?
It is the most common cause of N+1 queries, because a property access in a loop silently becomes a query. Many teams keep it off and load related data explicitly with Include or projection, so every query is visible in the code.
How do I find which queries are slow in production?
Log EF Core commands that exceed a threshold, check SQL Server's Query Store for the top queries by duration and reads, and add the missing indexes it suggests after reviewing them. Measure before and after each change.