Skip to content

Conversation

@roji
Copy link
Member

@roji roji commented Jan 20, 2026

Backports #37430
Fixes #37409

Description

When using Database.SqlQueryRaw<T>() or FromSqlRaw() with DbParameter instances followed by GroupBy and a Select projection, 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 SqlQueryRaw or FromSqlRaw with 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.

@roji roji requested a review from artl93 January 20, 2026 12:58
@roji roji changed the title [release/10.0] Fix duplicate DbParameter naming uniquification (#37430) [release/10.0] Fix duplicate DbParameter naming uniquification Jan 20, 2026
@roji roji marked this pull request as ready for review January 20, 2026 14:57
@roji roji requested a review from a team as a code owner January 20, 2026 14:57
Copilot AI review requested due to automatic review settings January 20, 2026 14:57
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR backports a fix for issue #37409, which addresses a regression in EF Core 10.0 where DbParameter instances get their names uniquified multiple times when SQL tree fragments are duplicated (e.g., in GroupBy translation). This causes invalid SQL generation where parameter names in subqueries don't match the renamed parameters.

Changes:

  • Added caching mechanism to prevent duplicate processing of the same DbParameter instance
  • Implemented quirk switch for backward compatibility
  • Added test coverage for the GroupBy scenario that triggers the issue

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/EFCore.Relational/Query/Internal/RelationalParameterProcessor.cs Core fix: Added _processedDbParameters dictionary to cache processed DbParameters and prevent duplicate uniquification
test/EFCore.Relational.Specification.Tests/Query/FromSqlQueryTestBase.cs Added test case FromSql_GroupBy_non_reducing_Select that verifies the fix for the reported issue
test/EFCore.SqlServer.FunctionalTests/Query/FromSqlQuerySqlServerTest.cs Added SQL Server-specific test baseline verifying correct SQL generation with single parameter reference

Copy link
Member

@artl93 artl93 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regression from 9.0.x; multiple customers reported; straightforward repro; approved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SqlQuery generates invalid SQL with unreferenced parameter names due to parameter duplication

2 participants