[release/10.0] Fix duplicate DbParameter naming uniquification #37541
+85
−6
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.
Backports #37430
Fixes #37409
Description
When using
Database.SqlQueryRaw<T>()orFromSqlRaw()withDbParameterinstances followed byGroupByand aSelectprojection, EF Core 10.x generates invalid SQL where the parameter is renamed at the outer query level but the inner subqueries still reference the original parameter name. This also affects simpler scenarios where the same DbParameter is referenced multiple times in a FromSql query.The issue occurs because when EF Core duplicates SQL tree fragments (e.g., in GroupBy translation), the same DbParameter instance gets referenced from multiple FromSqlExpressions, and its name gets uniquified multiple times, causing the parameter name to be modified repeatedly.
In addition, simpler scenarios have been flagged that make this more suitable for patching.
Customer impact
Applications using
SqlQueryRaworFromSqlRawwith DbParameter instances in combination with GroupBy or when the same DbParameter is referenced multiple times will encounter runtime failures with errors like "column does not exist" or "parameter not found".Workarounds exist but are enough of a pain (as well as undiscoverable) so as to IMHO justify this for patching.
How found
Customer reported via GitHub issue #37409. Multiple customers have been affected based on issue comments and related reports (npgsql/efcore.pg#3703).
Regression
Yes, this is a regression introduced in EF Core 10.0 by the parameter handling changes in PR #35200. The same code works correctly in EF Core 9.0.10.
Testing
Added.
Risk
Low. The fix is minimal and targeted. Quirk implemented.