What is change tracking in EF Core?
Change tracking is EF Core's mechanism for monitoring entity instances and their state so it can determine which INSERT, UPDATE, or DELETE commands are required when SaveChanges is called.
Entity Framework Core interview questions covering change tracking, disconnected updates, transactions, savepoints and optimistic concurrency.
Open a question for the full answer, code and interview guidance.
Change tracking is EF Core's mechanism for monitoring entity instances and their state so it can determine which INSERT, UPDATE, or DELETE commands are required when SaveChanges is called.
EF Core commonly uses snapshot change detection. When an entity starts being tracked, EF Core records original values. During change detection it compares current values with those originals to determine which properties changed.
ChangeTracker provides access to information and operations for entities currently tracked by a DbContext. It can inspect entries, entity states, original/current values, tracked relationships, and change-detection behavior.
For tracked entities, EF Core can maintain the values originally loaded from the database and the values currently present on the entity.
Each tracked property entry exposes an IsModified flag. EF Core sets it when change detection identifies a modification, and application code can also set it explicitly.
AsNoTracking tells EF Core not to track entities returned by a query. It is well suited to read-only pages, reports, and API responses where the entities will not be modified through the same context.
AsNoTrackingWithIdentityResolution performs a no-tracking query while ensuring that repeated occurrences of the same entity identity in the query result are represented by the same object instance within that result materialization.
A disconnected scenario occurs when an entity is read using one DbContext but later modified or submitted when that context no longer exists. ASP.NET Core web applications commonly work this way because each request typically has a separate scoped DbContext.
Update generally marks the entity and appropriate reachable graph members as modified. With client-supplied models, this can cause more columns or related entities to be updated than intended.
A stub entity containing the key can be attached, and selected properties can be marked as modified. This can avoid a preliminary SELECT when the application already has enough trustworthy information.
ExecuteUpdate performs a set-based database UPDATE directly from a LINQ query without first loading matching entities into the change tracker.
ExecuteDelete performs a set-based DELETE directly in the database for rows matching a LINQ query. The entities do not need to be loaded or tracked first.
A database transaction groups operations into a unit that either commits successfully or is rolled back. Transactions protect consistency when multiple related database changes must succeed together.
For relational databases that support transactions, EF Core normally wraps the changes in a single SaveChanges call in a transaction. If a command fails, the transaction can roll back so the group of changes is not partially committed.
Use Database.BeginTransaction or BeginTransactionAsync to create a transaction. Perform the required operations, then explicitly commit. If an exception occurs before commit, roll back or allow disposal to end the uncommitted transaction according to provider behavior.
A savepoint marks a point inside an existing transaction to which work can be rolled back without necessarily aborting the entire transaction.
Optimistic concurrency assumes conflicting updates are relatively uncommon. Instead of locking a row for the entire user workflow, EF Core includes a concurrency token in the UPDATE or DELETE condition.
A concurrency token is a property whose original value participates in the condition used for UPDATE or DELETE operations. It helps EF Core detect whether the database row changed since the entity was originally read.
SQL Server rowversion is a database-generated binary value that changes when a row is updated. EF Core can map it as a concurrency token.
Catch DbUpdateConcurrencyException around SaveChanges when the application uses optimistic concurrency. Inspect the affected entries and retrieve current database values if needed.
Optimistic concurrency allows multiple users to read data without holding long-lived locks and detects conflicts when an update is attempted. EF Core directly supports this pattern through concurrency tokens.
A database transaction cannot normally make an external HTTP service participate in the same atomic local transaction. Holding a database transaction open while waiting for an external API also increases lock duration and failure complexity.
The outbox pattern stores a business data change and a message-to-be-published in the same database transaction. After commit, a background process reads the outbox and publishes the message to the external broker or service.
A DbContext can inspect tracked entries before SaveChanges and populate fields such as CreatedAtUtc and UpdatedAtUtc. This centralizes common persistence metadata instead of repeating it in every service.
Use tracked entities for straightforward unit-of-work updates, but use no-tracking queries for read-only workloads. In disconnected web requests, load the authoritative entity and map only permitted changes instead of blindly updating client-supplied graphs.