In Entity Framework Core (EF Core), query performance and memory management are heavily governed by one underlying mechanism: Change Tracking.
While EF Core's default tracking behavior makes data mutations seamless, applying it blindly across read-only queries can introduce significant memory and CPU overhead. Understanding how the ChangeTracker operates under the hood—and knowing when to bypass it using AsNoTracking() or AsNoTrackingWithIdentityResolution()—is essential for building high-performance .NET applications.
1. How Default Change Tracking Works
When EF Core executes a standard LINQ query, it does far more than map SQL result sets to C# objects. It engages a complex tracking infrastructure through the DbContext instance.
// Standard tracking query
var user = await dbContext.Users.FirstOrDefaultAsync(u => u.Id == 1);The Query Execution Lifecycle
When executing the code above, EF Core carries out four sequential operations:
SQL Execution: Sends the generated SQL
SELECTstatement to the database server.Object Materialization: Instantiates the C# entity class (
User) and populates its properties from the SQL data reader.Identity Resolution: Checks the
DbContext's internal Identity Map. If an entity with the same primary key is already being tracked, EF Core discards the newly materialized instance and returns the existing tracked reference.Snapshot Creation: Creates an internal snapshot copy of the entity's initial values inside the
ChangeTracker.
What Happens During SaveChangesAsync()?
When SaveChangesAsync() is called, EF Core initiates a process known as Detect Changes. It scans all tracked entities and compares their current property values against the snapshots taken during Step 4.
var user = await dbContext.Users.FirstOrDefaultAsync(u => u.Id == 1);
user.Email = "[email protected]"; // Property updated
// DetectChanges compares state to snapshot -> generates UPDATE SQL statement
await dbContext.SaveChangesAsync();The Cost of Tracking
Double Memory Footprint: Every tracked entity requires memory for both the active instance and its hidden initial snapshot.
CPU Overhead:
DetectChanges()must run a property-by-property equality comparison across every tracked object in memory before generating SQL.
2. Bypassing the Change Tracker with AsNoTracking()
When fetching data strictly for read-only operations (such as populating a UI view or returning data from a REST API GET endpoint), change tracking is unnecessary.
var user = await dbContext.Users
.AsNoTracking()
.FirstOrDefaultAsync(u => u.Id == 1);Under the Hood Mechanics
When .AsNoTracking() is appended to a query, EF Core alters its execution pipeline:
No Snapshots: Objects are materialized and returned immediately without making snapshot copies.
No
DbContextRegistration: TheChangeTrackerremains unaware of the object's existence.Faster Garbage Collection: Memory overhead is halved, allowing allocated memory to be reclaimed faster by the .NET Garbage Collector.
The Trade-Off: Identity Resolution Loss
In AsNoTracking() queries involving .Include() joins, EF Core bypasses the Identity Map entirely. This can lead to duplicate object instances in memory.
var orders = await dbContext.Orders
.AsNoTracking()
.Include(o => o.Customer)
.ToListAsync();If Order 1 and Order 2 belong to Customer A, standard tracking would map both orders to a single shared Customer instance in RAM. With .AsNoTracking(), EF Core creates two separate Customer instances in memory with identical values.
3. The Middle Ground: AsNoTrackingWithIdentityResolution()
To solve object duplication in complex read-only queries with multiple joins, EF Core introduced AsNoTrackingWithIdentityResolution().
var orders = await dbContext.Orders
.AsNoTrackingWithIdentityResolution()
.Include(o => o.Customer)
.ToListAsync();
Join the conversation! Your thoughts help the community grow.