Learn / .NET API Engineering Path
EF Core Performance Boundaries
Load the data you need, track only what you change, and spot query problems before production.
Course: .NET API Engineering Path. Level: Advanced. Topic: Typed backend.
Stage: advanced - Advanced data access - EF Core query design and performance review. Treat data access as a reviewable system: projections, tracking choices, limits, and generated SQL are part of the contract.
Outcomes
- Use projections for read models.
- Understand tracking costs.
- Inspect generated SQL in review.
Concepts
- projection
- tracking
- include
- compiled query
Concept flow
Show how ef core query design and performance review moves from trigger to implementation outcome in .NET APIs.
- Controller
- Query object
- Projection
- Database
- DTO
Session flow
- Model projection (concept, 10 min) — Name the decisions behind projection before writing code.
- Use projections for read models.
- Explain where projection belongs in order management API.
- Build the vertical slice (walkthrough, 18 min) — Implement the smallest useful slice in OrdersQuery.cs.
- Understand tracking costs.
- Connect tracking to a working example.
- Verify and harden (exercise, 12 min) — Add pagination to a list route.
- Inspect generated SQL in review.
- Record one risk or follow-up before moving on.
Code example
C# in OrdersQuery.cs.
public static Task<List<OrderRow>> RecentOrders(AppDbContext db, CancellationToken ct) =>
db.Orders
.AsNoTracking()
.OrderByDescending(order => order.CreatedAt)
.Select(order => new OrderRow(order.Id, order.CustomerEmail, order.TotalCents))
.Take(50)
.ToListAsync(ct);
Walkthrough examples
- EF Core Performance Boundaries in a order management API — A team is extending a production-grade ASP.NET Core order API and needs this lesson's pattern to be clear enough for review, testing, and future maintenance.
- File: OrdersQuery.cs
- File: src/Orders.Application/ef-core-performance.cs
- File: tests/Orders.Api.Tests/ef-core-performance.Tests.cs
- File: docs/dotnet-advanced/ef-core-performance.md
- Start from the provided C# snippet and make the intent visible in names and boundaries.
- Apply the checklist item "Project to DTO" before adding extra behavior.
- Write down how the implementation changes when tracking fails or becomes slow.
Practice
- Replace one Include-heavy read with a projection.
- Log SQL for the endpoint.
- Add pagination to a list route.
Checklist
- Project to DTO
- Use AsNoTracking
- Limit result size
- Inspect SQL
Quiz prompts
- Why use AsNoTracking for read-only queries? — Read-only projections usually do not need EF Core change tracking.
- A teammate wants to hide projection inside a convenient helper. What should you check first? — Place projection at the boundary that keeps order management API behavior explicit, testable, and reviewable.
- Which artifact best proves this .NET APIs lesson is ready for review? — Production-ready learning needs evidence: a test, trace, command, screenshot, or log that catches the risk again.
- Advanced data access: a teammate says the happy path works, but "Read model projections" is still implicit. What should you ask for before merging? — Read model projections belongs in the advanced stage only when the decision is visible, testable, and tied to a realistic failure mode.
- A reviewer has five minutes to evaluate this advanced .NET APIs slice. Which evidence is strongest? — Upgrade the order list query into an advanced read model with pagination, no-tracking projection, SQL review notes, and one performance regression guard.
Flashcards
- Advanced data access: what decision does "Read model projections" force you to make? Build DTO-shaped queries that load only what the screen or API contract needs. Evidence prompt: Replace an entity graph read with a projection and capture the generated SQL.
- Advanced data access: what decision does "Query risk review" force you to make? Identify tracking overhead, unbounded result sets, and accidental includes before they reach production. Evidence prompt: Write a query review note that calls out cardinality, indexes, and tracking behavior.
- Advanced data access: what decision does "Indexes, pagination, and N+1 review" force you to make? Tie API pagination rules to database indexes and prove no hidden per-row query appears under load. Evidence prompt: Add an index or pagination constraint and capture before/after SQL for the riskiest list endpoint.
- In .NET APIs, what should you remember about projection? projection matters here because it supports "Use projections for read models.".
- In .NET APIs, what should you remember about tracking? tracking matters here because it supports "Understand tracking costs.".
- In .NET APIs, what should you remember about include? include matters here because it supports "Inspect generated SQL in review.".
- In .NET APIs, what should you remember about compiled query? compiled query matters here because it supports "Use projections for read models.".
Labs
- Ship a ef core performance boundaries slice — Extend a production-grade ASP.NET Core order API with a small but reviewable feature that proves the lesson's architecture in code.
- Upgrade the order list query into an advanced read model with pagination, no-tracking projection, SQL review notes, and one performance regression guard.
- Replace an entity graph read with a projection and capture the generated SQL.
- Write a query review note that calls out cardinality, indexes, and tracking behavior.
- Add an index or pagination constraint and capture before/after SQL for the riskiest list endpoint.
- Replace one Include-heavy read with a projection.
- Log SQL for the endpoint.
- The lab demonstrates the advanced data access outcome without skipping earlier contract evidence.
- Each subtopic has a concrete artifact: Read model projections, Query risk review, Indexes, pagination, and N+1 review.
- The review notes explain how this level changes ownership, verification, or operational risk.
Challenge
- Review-ready ef core performance boundaries (Core) — Upgrade the order list query into an advanced read model with pagination, no-tracking projection, SQL review notes, and one performance regression guard.
- All checklist items are either implemented or documented with a reason.
- The change can be understood from OrdersQuery.cs plus one short note.
- The concept diagram names ownership, failure handling, and verification points.
- A teammate could run the verification steps without asking for hidden context.
Canonical lesson URL