ExecuteUpdate over projection uses derived-table copy of updated column, causing possible lost updates
Maintainers usually reply within 2 days
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 52/100
Research direction
Start by reproducing the supplied ExecuteUpdateAsync LINQ query and inspecting the generated PostgreSQL SQL. Trace the ExecuteUpdate translation path to determine why the derived table supplies the updated column; done means the SET expression references the update target alias and concurrent increments preserve the combined total.
Written by the indexing model from the issue text.
Description
Bug description
ExecuteUpdate over a projection can generate SQL where an increment expression uses a value read into a derived table instead of referencing the actual update target alias.
This can cause lost updates under concurrent executions.
The LINQ expression represents an atomic increment:
target.TotalQuantity = target.TotalQuantity + delta
But the generated SQL uses:
SET "TotalQuantity" = s2."TotalQuantity" + s2."QuantityDelta"
where s2."TotalQuantity" is read inside a derived table before the update.
For concurrent counter updates, the generated SQL should reference the update target alias:
SET "TotalQuantity" = s1."TotalQuantity" + s2."QuantityDelta"
because s1 is the actual row being updated.
Example lost-update scenario:
Initial TotalQuantity = 100
Transaction 1 delta = 10
Transaction 2 delta = 20
Both transactions can read s2.TotalQuantity = 100.
Transaction 1 updates row to 110.
Transaction 2 waits for the same row, then updates row to 100 + 20 = 120.
Expected final value: 130.
Possible final value: 120.
Your code
await context.ShortageOrderLines
.Where(line => line.ShortageOrderId == orderId)
.GroupBy(line => line.ShortageArticleTask!)
.Select(g => new
{
Task = g.Key,
QuantityDelta = g.Sum(l => l.Quantity)
})
.ExecuteUpdateAsync(setters => setters.SetProperty(
t => t.Task.TotalQuantity,
t => t.Task.TotalQuantity + t.QuantityDelta));
Generated SQL:
UPDATE shortage."ShortageArticleTasks" AS s1
SET "TotalQuantity" = s2."TotalQuantity" + s2."QuantityDelta"
FROM (
SELECT s0."Id", s0."TotalQuantity", COALESCE(sum(s."Quantity"), 0.0) AS "QuantityDelta"
FROM shortage."ShortageOrderLines" AS s
LEFT JOIN shortage."ShortageArticleTasks" AS s0 ON s."ShortageArticleTaskId" = s0."Id"
WHERE s."ShortageOrderId" = @orderId
GROUP BY s0."Id", s0."ArticleId", s0."LogisticVariantId", s0."TotalQuantity"
) AS s2
WHERE s1."Id" = s2."Id"
Expected SQL shape:
UPDATE shortage."ShortageArticleTasks" AS s1
SET "TotalQuantity" = s1."TotalQuantity" + s2."QuantityDelta"
FROM (
SELECT
s."ShortageArticleTaskId",
COALESCE(sum(s."Quantity"), 0.0) AS "QuantityDelta"
FROM shortage."ShortageOrderLines" AS s
WHERE s."ShortageOrderId" = @orderId
GROUP BY s."ShortageArticleTaskId"
) AS s2
WHERE s1."Id" = s2."ShortageArticleTaskId"
Stack traces
No exception is thrown. The generated SQL executes successfully, but it may have incorrect concurrency semantics for counter increments.
EF Core version
10.0.8
Database provider
Npgsql.EntityFrameworkCore.PostgreSQL
Target framework
net10.0
Operating system
Amazon Linux
IDE
VS 2026
- Dominant language
- C#
- Stars
- 14.8k
- Forks
- 3.5k
- Avg merge
- 3d 7h
- Merged PRs (30d)
- 182
Getting set up
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dotnet/efcore
-
customer-reported
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 2 days
-
GroupBy element `OrderBy(...).First()`/`MaxBy` over an owned type mapped to its own table fails to translatePossibly taken @cincuranet claimed this today. Openarea-groupby area-query
Difficulty 4/5 3-5 days Newbie friendliness 25/100
dotnet/efcore#39833 · 1 assignee ·
Maintainers usually reply within 2 days
-
SQLite: Polygon.Length translates to GLength, which returns 0Possibly taken @cincuranet claimed this today. Openarea-query area-sqlite
Difficulty 3/5 1-2 days Newbie friendliness 45/100
dotnet/efcore#39826 · 1 assignee ·
Maintainers usually reply within 2 days
-
Migration lock with LockReleaseBehavior.Transaction doesn't prevent concurrent migrations since per-migration transactionsPossibly taken @AndriySvyryd claimed this 1 day ago. Openarea-migrations
dotnet/efcore#39824 · 1 assignee ·
Maintainers usually reply within 2 days
-
Querying a TPC base type with a JSON complex property throwsPossibly taken @cincuranet claimed this 1 day ago. Openarea-query
Difficulty 4/5 3-5 days Newbie friendliness 22/100
dotnet/efcore#39814 · 1 assignee ·
Maintainers usually reply within 2 days
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
SubtitleEdit/subtitleedit#17247 ·
Maintainers usually reply within 1 day
-
Restore test cleanup fails on Windows when restored files are read-onlyPossibly taken @JamBalaya56562 claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
duplicati/duplicati#8046 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
Maintainers usually reply within 1 day
-
Bundles Documentation
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
pyrevitlabs/pyRevit#4285 ·
Maintainers usually reply within 1 day