Reverse processing order of setters in query generator #37323
+28
−13
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Refactor processing of method call expressions in setters.
Problem it solves:
ExecuteUpdateAsyncwithSetPropertychain generates reversed parameters in AOT Interceptors, causing InvalidCastException.When running
dotnet ef dbcontext optimizewith--precompile-queriesand--nativeaot, the generated interceptor code for anExecuteUpdateAsyncchain appears to process theSetPropertycalls in reverse order (Last-In-First-Out) instead of source order.This results in a mismatch between the parameter expected by the generated SQL and the value supplied by the interceptor.
Steps to Reproduce:
ExecuteUpdateAsynccall with multipleSetPropertycalls of different types.dotnet ef dbcontext optimize --precompile-queries --nativeaot ...Expected Behavior: The interceptor should map
Expressions[0]to the first property (IsDeleted) andExpressions[1]to the second (LastModified), matching the source order.Actual Behavior: The generated code assumes
Expressions[0]corresponds to the last property written (LastModified/now), effectively reversing the parameters.Runtime Exception: Because
Expressions[0](Boolean) is being passed into the parameter expectingDateTime(TimestampTz), Npgsql throws a cast exception:Proposed Fix: The issue appears to be in
PrecompiledQueryCodeGenerator.ProcessExecuteUpdate. The method iterates over theMethodCallExpressiontree (which is nested inside-out) but does not reverse the order before building the setters array.Using a
Stack<MethodCallExpression>to collect and then reverse the calls fixes the issue by ensuring setters are added in source code order (First -> Last).