Change tracking and SaveChanges

1 min read
On trails:EFCOREstep 5/8

When entities are queried from the database through a DbContext, EF Core by default keeps track of their original state. When properties of those entities are modified, new entities are added to a DbSet, or existing entities are marked for deletion, the DbContext records those changes.

The SaveChanges() or SaveChangesAsync() method on the DbContext examines the tracked changes and generates the INSERT, UPDATE or DELETE SQL statements needed to persist them to the database. These operations are typically wrapped in a database transaction, to ensure atomicity.

Tracking has a cost. For read-only queries, see AsNoTracking() in the query habits noteEF Core query habitsSix habits that keep EF Core queries fast.Opens in a new tab.