EF Core optimistic concurrency: preventing lost updates with concurrency tokens (SQL)

EF Core Interview Q&A November 13, 2026 sql

By default: last write wins.

sql
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
-- ======================================================================
-- Q: Two users edit the same product at the same time. What happens and how do
-- you prevent it?
--
-- By default: last write wins. The second user overwrites the first user's
-- change without knowing it (lost update). Solution: optimistic concurrency with
-- a concurrency token.
-- EF Core puts the token in the WHERE clause. If it changed in the meantime, 0
-- rows are affected and it throws DbUpdateConcurrencyException.
-- In an API: the version goes to the client (ETag / version field) and comes
-- back. On conflict: 409 Conflict (or 412 with If-Match), and the user reloads.
-- In a background job: reload and retry (or merge per field).
-- ======================================================================

-- UPDATE generated by EF Core with the concurrency token in WHERE
UPDATE products SET list_price = @p0
WHERE id = @p1 AND xmin = @p2;        -- 0 rows → DbUpdateConcurrencyException