Entity Framework Core Mastery

Tracking vs No-Tracking Queries (Performance)

1 Views Updated 5/4/2026

Tracking vs No-Tracking Queries (Performance)

When EF Core downloads data from SQL Server, it doesn't just hand you the C# objects. By default, it takes a "snapshot" of those objects and stores them inside the DbContext ChangeTracker memory. If you modify the object later, EF Core compares the object against its snapshot to figure out what SQL UPDATE statement to write. This snapshotting process consumes massive amounts of CPU and RAM.

1. The Performance Cost of Tracking

If you query 10,000 products just to display them on a "Catalog" webpage, you are completely wasting memory. You have absolutely no intention of Updating or Deleting those products; they are read-only. Yet EF Core still painstakingly builds 10,000 snapshots in the background.

2. Applying AsNoTracking()

To eliminate this overhead, you append the .AsNoTracking() method to your LINQ query. This tells EF Core: "Just give me the raw data. Do not track it. Do not snapshot it."

// ❌ BAD: Wastes RAM by tracking objects we only meant to read
var products = await _context.Products.ToListAsync();

// ✅ GOOD: Bypasses the Change Tracker completely! Up to 40% faster on large queries.
var optimizedProducts = await _context.Products.AsNoTracking().ToListAsync();

3. Global No-Tracking Configurations

If you are building a pure "Reporting API" where almost every single query is read-only, you don't want to type .AsNoTracking() 500 times. You can change the default behavior of the entire DbContext.

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddDbContext<ApplicationDbContext>(options =>
{
    options.UseSqlServer("...");
    // Every query executed by this context will default to No-Tracking!
    options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking); 
});
Revising Global Rules: If you altered the default to No-Tracking, but you suddenly need one specific query to be tracked so you can update a user, you explicitly append .AsTracking() to that specific LINQ query.

4. Interview Mastery

Q: "We queried a User using `.AsNoTracking()`. Later in the same method, we decided we DO want to update the user's name. So we changed the name on the untracked C# object and called `await _context.SaveChangesAsync()`. Why did the database not update?"

Architect Answer: "Because EF Core has absolutely no idea that the object exists. When you use `AsNoTracking()`, the object is completely orphaned from the DbContext the moment it is downloaded. Calling `SaveChanges()` literally does nothing because the Change Tracker's internal list of 'Modified Entities' is completely empty. If you want to update an untracked entity, you must explicitly re-attach it to the DbContext using `_context.Update(user);`. This forces EF Core to start tracking the object again and marks its state as Modified, ensuring that `SaveChanges` generates the SQL `UPDATE` statement."

Entity Framework Core Mastery
1. Foundations & Architecture
Introduction to Object Relational Mapping (ORM) Entity Framework Core Architecture & Providers Setup and DbContext Integration Code-First vs Database-First Approaches Reverse Engineering Existing Databases (Scaffolding)
2. Code-First Modeling
Entity Conventions & Data Annotations The Fluent API Deep Dive (OnModelCreating) Primary Keys, Composite Keys, & Guids Required Properties & Database Defaults Value Conversions (Enums & Strongly Typed IDs)
3. Relational Architecture
One-to-Many Relationships & Foreign Keys One-to-One Relationships (Dependent Entities) Many-to-Many Relationships & Navigation Properties Owned Entity Types (Value Objects) Table-per-Hierarchy (TPH) Inheritance
4. Data Querying & LINQ
Basic LINQ Queries & IQueryable Execution Tracking vs No-Tracking Queries (Performance) Eager Loading vs Explicit Loading (Include) Lazy Loading Pitfalls & Proxies Client vs Server Evaluation Parsing
5. Manipulating Data (CUD)
Adding, Updating, and Removing Entities The ChangeTracker and Entity States Disconnected Entities in Web APIs Batch Updates and Deletes (.NET 7+)
6. Advanced Performance & Scale
Concurrency Tokens and Optimistic Locking Raw SQL Queries and Views (FromSqlRaw) Compiled Queries for High Throughput Interceptors (Logging & Auditing Data Changes) DbContext Pooling Mechanisms Managing Complex EF Core Migrations (CI/CD)