ASP.NET Core MVC Interview Question #179
How would you handle concurrent updates to the same record?
Senior Practical MVC Scenarios & Production Architecture Senior Advanced
Quick Interview Answer
Use an optimistic concurrency mechanism such as an EF Core concurrency token or rowversion. When an update detects that another user changed the record after it was loaded, catch the concurrency conflict and decide whether to reload, merge, or ask the user to review the latest data.
Detailed Explanation
Use an optimistic concurrency mechanism such as an EF Core concurrency token or rowversion. When an update detects that another user changed the record after it was loaded, catch the concurrency conflict and decide whether to reload, merge, or ask the user to review the latest data.
Do not silently overwrite another user's changes unless that behavior is explicitly required.
Code Example
try
{
await db.SaveChangesAsync(token);
}
catch (DbUpdateConcurrencyException)
{
ModelState.AddModelError(
"",
"This record was changed by another user. Reload and try again.");
return View(model);
}