Tutorials Entity Framework Core Tutorial

Compiled Queries in EF Core

Compiled Queries 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 54 of 100

Compiled Queries in EF Core

Beginner ✓Intermediate ✓AdvancedProfessional

Advanced · 3 — Production skills · ~10 min · Module 6: Advanced EF Core

What is this?

Compiled queries cache the LINQ translation plan so EF skips recompiling the same query shape on every call — defined with EF.CompileAsyncQuery or EF.CompileQuery.

Why should you care?

ShopNest product-by-id lookup runs millions of times daily — compiled query shaves CPU on hot paths.

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

private static readonly Func<ShopNestDbContext, int, Task<Product?>> GetProductById =
    EF.CompileAsyncQuery((ShopNestDbContext db, int id) =>
        db.Products.AsNoTracking().FirstOrDefault(p => p.Id == id));

// Usage:
var product = await GetProductById(_context, productId);

What happened?

  • CompileAsyncQuery builds expression once at static initialization.
  • Each call reuses compiled delegate — parameters passed separately.

Practice next

  1. Identify extremely hot queries with stable shape.
  2. Extract to static readonly compiled delegate.
  3. Pass DbContext instance per call — still scoped.
  4. Compile category product list query with categoryId parameter.
  5. Compare TagWith logged compile time first vs subsequent calls.

Remember

Compiled queries reuse translation plans. Best for fixed-shape hot reads. Pass context as parameter each call.

ShopNest PDP hot path

Product detail microservice uses compiled GetProductById for cache miss database fallback.

Outcome: Microseconds saved per request aggregate to meaningful CPU savings at scale.

Interview prep for this lesson

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

Junior PDF Detailed
What is the difference between tracked and untracked queries?
Short answer: Tracked queries: EF Core tracks entities returned from the query. Changes to them are monitored and persisted. Untracked queries: EF does not monitor the returned entities. Faster, read-only access. Tracked…
Junior PDF Detailed
What is the difference between tracked and untracked queries?
Short answer: re monitored and persisted. Untracked queries: EF does not monitor the returned entities. re monitored and persisted. Untracked queries: EF does not monitor the returned entities. Faster, read-only ccess. T…
Junior PDF Detailed
What is Entity Framework Core?
Short answer: Entity Framework Core (EF Core) is a modern, lightweight, cross-platform, open-source ORM (Object-Relational Mapper) for .NET. It allows developers to interact with databases using C# objects rather than SQ…
Mid PDF Detailed
What are the main differences between Entity Framework (EF6) and EF Core?
Short answer: Feature EF6 EF Core Platform .NET Framework only Cross-platform (.NET Core) Performance Slower Faster and more optimized LINQ support Limited Enhanced Change tracking Basic More efficient Migrations Availab…
Junior PDF Detailed
What is ORM (Object-Relational Mapping)?
Short answer: How does EF Core implement it? ORM is a technique that maps objects in code to relational database tables. EF Core implements ORM by mapping .NET classes (entities) to database tables using DbContext, DbSet…
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