Entity Framework Core Mastery
Lesson 22 of 30 73% of course

The ChangeTracker and Entity States

18 · 8 min · 5/23/2026

Sign in to track progress and bookmarks.

The ChangeTracker and Entity States

The ChangeTracker is the internal brain of Entity Framework Core. When you call SaveChanges(), EF Core doesn't inspect the database; it inspects the ChangeTracker. The tracker assigns a specific EntityState (Added, Modified, Deleted, Unchanged, Detached) to every object it manages.

1. Understanding the 5 Entity States

The SQL that gets generated is 100% determined by the object's current state.

EntityState When it happens SaveChanges Output
DetachedThe object was created locally, or queried with AsNoTracking.Ignored. No SQL executed.
UnchangedThe object was queried, but no C# properties were altered.Ignored. No SQL executed.
AddedYou called _context.Add().INSERT INTO statement.
ModifiedYou altered a property, or called _context.Update().UPDATE statement.
DeletedYou called _context.Remove().DELETE FROM statement.

2. Inspecting the Tracker Manually

You can debug exactly what EF Core is planning to do by looking directly into the ChangeTracker.

var user = await _context.Users.FindAsync(1); // State = Unchanged
user.Name = "Sandeep";                    // State instantly becomes 'Modified'

// Manually verify the state:
var trackingInfo = _context.Entry(user);
Console.WriteLine(trackingInfo.State); // Prints: Modified

// See exactly WHICH properties changed!
var nameProp = trackingInfo.Property(u => u.Name);
Console.WriteLine($"Original: {nameProp.OriginalValue}, Current: {nameProp.CurrentValue}");

3. Forcing State Changes

You don't have to rely on automatic tracking. You can forcefully dictate the state of an object to bypass EF Core's built-in change detection.

var user = new User { Id = 1, Name = "Force Update" };

// Attach the object and violently FORCE its state to Modified
_context.Entry(user).State = EntityState.Modified;

// Generates an UPDATE statement for ALL columns, because it assumes the whole object is altered
await _context.SaveChangesAsync();

4. Interview Mastery

Q: "We queried a User object. We need to update their `DateOfBirth`, but we DO NOT want to update their `PasswordHash`, even though a rogue developer might accidentally alter the password property in C#. How do we use the ChangeTracker to lock down specific properties from generating SQL updates?"

Architect Answer: "You can manually manipulate the `IsModified` flag on specific properties within the ChangeTracker. Once the object is tracked, you can write: `_context.Entry(user).Property(u => u.PasswordHash).IsModified = false;`. When you call `SaveChanges()`, EF Core scans the object. Even if the password property was egregiously mutated in C# memory, EF Core will explicitly ignore it because you overrode the tracker string. The generated SQL `UPDATE` statement will completely omit the `PasswordHash` column, guaranteeing data integrity at the framework level."

Test your knowledge

Quizzes linked to this course—pass to earn certificates.

Browse all quizzes
Entity Framework Core Mastery

On this page

1. Understanding the 5 Entity States 2. Inspecting the Tracker Manually 3. Forcing State Changes 4. Interview 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)