efcore
efcore copied to clipboard
Precompiled ExecuteUpdate reads multi-setter values in reverse order, writing them to the wrong columns
Summary
With precompiled queries, a multi-setter ExecuteUpdate reads its setter values in reverse order. Each value is written into another setter's column. Same-typed columns swap silently; differently typed columns get a wrong-typed value.
Repro
await ctx.Set<E>()
.Where(e => e.Id == id)
.ExecuteUpdateAsync(s => s
.SetProperty(e => e.A, a)
.SetProperty(e => e.B, b));
A and B are both string. Precompile the query and publish NativeAOT.
- Expected (JIT):
A = a,B = b - Actual (precompiled, NativeAOT):
A = b,B = a
Analysis
src/EFCore.Design/Query/Internal/PrecompiledQueryCodeGenerator.cs:1166-1210:ProcessExecuteUpdatestarts at the outermostSetPropertycall and walksmethodCallExpression.Object(line 1203), so setteriis counted from the last call. The extraction paths it emits (:751-776,:805-835) readsetters.Expressions[i]with that index.src/EFCore/Query/UpdateSettersBuilder.cs:35-36,45-53,63-79: at runtime the builder keeps the calls in evaluation order (first call first).
For N setters the generated index has to be N - 1 - i.
Workaround
We post-process the generated interceptors and rewrite each setters.Expressions[i] to setters.Expressions[setters.Expressions.Count - 1 - i]. A row-level check (native against JIT) confirms the values then land in the right columns.
Environment
- EF Core 10.0.7 (
Microsoft.EntityFrameworkCore.Sqlite10.0.7,Microsoft.EntityFrameworkCore.Tasks10.0.7) - .NET SDK 10.0.403,
PublishAot=true,EFOptimizeContext=true,EFPrecompileQueriesStage=build, win-x64 - Provider: SQLite
- Source references below are to the
v10.0.7tag.
Fixed in https://github.com/dotnet/efcore/pull/37323