efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Precompiled ExecuteUpdate reads multi-setter values in reverse order, writing them to the wrong columns

Open rockyway opened this issue 1 month ago • 1 comments

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: ProcessExecuteUpdate starts at the outermost SetProperty call and walks methodCallExpression.Object (line 1203), so setter i is counted from the last call. The extraction paths it emits (:751-776, :805-835) read setters.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.Sqlite 10.0.7, Microsoft.EntityFrameworkCore.Tasks 10.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.7 tag.

rockyway avatar Oct 27 '26 13:10 rockyway

Fixed in https://github.com/dotnet/efcore/pull/37323

AndriySvyryd avatar Oct 30 '26 23:10 AndriySvyryd