See it
Load an order, bulk-update the same row, then read the tracked instance:
var order = await db.Orders.FirstAsync(o => o.Id == 42);
// order.Status is "Pending", and EF is tracking it
await db.Orders
.Where(o => o.Id == 42)
.ExecuteUpdateAsync(s => s.SetProperty(o => o.Status, "Shipped"));
// The row in the database now says "Shipped"
Console.WriteLine(order.Status); // still "Pending"
Why it bites
The tracked entity still holds the value EF loaded. Change that property on it and call SaveChanges, and EF compares against the loaded value, not the database. Your bulk update gets overwritten with no warning.
It also skips concurrency checks. EF does not add your concurrency token to the UPDATE, so you add that condition to the Where clause yourself.
What I do
- Keep bulk updates and tracked edits on the same rows out of the same unit of work.
- When I need the entity after a bulk update, I reload it.
- For rows with a concurrency token, I put the token check in the
Whereclause. IfExecuteUpdateAsyncreturns 0 rows, someone else changed the row first.
await db.Entry(order).ReloadAsync();
// order.Status is now "Shipped"