Entity Framework Core Interview Question #117

What is optimistic concurrency in EF Core?

Change Tracking, Updates, Transactions & Concurrency Mid-Level Intermediate

Quick Interview Answer

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.

Detailed Explanation

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.

If another process changed the row after it was read, the database command affects zero rows and EF Core can throw DbUpdateConcurrencyException.

The application then decides whether to reject, retry, merge, or show the current database values to the user.

Code Example

modelBuilder.Entity<Product>()
    .Property(p => p.RowVersion)
    .IsRowVersion();