Change Tracking

Learn the basics about change tracking in EF Core.

Overview

In this lesson, we’ll learn how EF Core tracks the changes we make to entities. Each DbContext instance tracks changes through the ChangeTracker. The ChangeTracker resides in the Microsoft.EntityFrameworkCore.ChangeTracking namespace and tracks the state of every entity instance retrieved with the same DbContext instance.

The projects in this lesson include data from the table below:

Employees

Id

FirstName

LastName

Age

1

Max

Bello

25

2

Francis

Ojukwu

34

3

Martha

Bertha

19

Albums

Id

Title

Price

EmployeeId

1

Blue Fire

2000

2

2

Raging Heart

3850

3

3

Fixated on You

4000

1

Note: The projects in this lesson make calls to ChangeTracker.DebugView.LongView. This allows us to obtain a human-readable view of the state of the tracked entities at a specific point in time.

Tracking entities

Entity instances become tracked when they are:

  • Returned from a query executed against the database.
  • Explicitly attached to the DbContext by Add, Attach, Update, or similar methods.
  • Detected as new entities connected to existing tracked entities.

EF Core stops tracking entity ...

Get hands-on with 1400+ tech skills courses.