Skip to content

Throw when same-named DbParameters are hard-coded in FromSql/SqlQuery SQL - #39593

Open
ajcvickers wants to merge 3 commits into
dotnet:mainfrom
ajcvickers:fix-32932-fromsql-duplicate-dbparameter-names
Open

ajcvickers wants to merge 3 commits into
dotnet:mainfrom
ajcvickers:fix-32932-fromsql-duplicate-dbparameter-names

Conversation

@ajcvickers

@ajcvickers ajcvickers commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #32932

When two DbParameters in the same query had the same name, the second was renamed to be unique. That is only correct when the SQL references the parameter positionally ({0}), since EF then writes the new name into the SQL. If the SQL hard-codes the name (for example SELECT @date AS Date), it kept pointing at the first parameter, so every subquery silently used the first value.

Change

A DbParameter whose name is written directly into the SQL passed to FromSqlRaw/SqlQueryRaw can't be renamed. RelationalParameterProcessor now collects those names before it processes the query, and every other parameter avoids them, whichever is encountered first:

  • a DbParameter referenced positionally ({n}, {n,w} or {n:fmt}; escaped {{n}} doesn't count) is renamed if its name is taken;
  • the parameters EF generates (p0, ..., and the ones named after captured variables, e.g. @city) skip hard-coded names.

Only when two different DbParameters both hard-code the same name does it throw an InvalidOperationException, because nothing can be renamed to make that work. Parameters are deliberately not treated as interchangeable when their values happen to be equal: deciding that is provider-specific, and sharing them would hide a coupling that silently breaks when one value later changes.

Unchanged: positional ({0}) and interpolated usage, nameless parameters, distinct names, and reusing the same DbParameter instance (#37409).

Known limitation: SQL that references the same argument both positionally and by name isn't detected.

Breaking change

Queries that relied on two DbParameters with the same name and the same value both being hard-coded in the SQL (which only worked by accident) now throw, with a message explaining the fix (distinct names, or positional placeholders). A hard-coded name that collides with a parameter EF generates, or with a positional DbParameter, no longer causes a problem: EF renames its own. Targeting main only; not for servicing. Needs the breaking-change label. Docs PR: dotnet/EntityFramework.Docs#5516

Tests

  • Base FromSqlQueryTestBase/SqlQueryTestBase tests (sync and async): same name, same name and value, distinct names, hard-coded then positional and positional then hard-coded (both work), prefixed vs unprefixed, case differences, collision with a generated name (works), a captured variable of the same name visited before or after the DbParameter (in a Union and in a predicate; works), compiled queries (the same compiled query throws on every execution; positional works on repeat execution), and SqlQueryRaw.
  • Unit test for the placeholder detection ({00}, escaped braces, {0,5}, {0:x}, whitespace before the index, etc.).
  • SQL Server baselines for the tests that execute SQL.

The names are collected in an extra walk over the SQL tree each time a command is built (it finds nothing for queries without FromSql).

Ran SQL Server and SQLite functional tests and the full test.cmd for the first two commits. Three unrelated failures in the parallel run (two ManyToMany tests and one Microsoft.Data.Sqlite busy-commit test) pass when re-run on their own. The follow-up commit was verified with the SQL Server and SQLite FromSqlQuery/SqlQuery test classes and the RelationalParameterProcessor unit tests.

🤖 Generated with Claude Code

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Copilot AI balanced review requested due to automatic review settings November 10, 2026 16:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

ajcvickers and others added 2 commits November 14, 2026 11:40
… SQL

When two DbParameters in the same query had the same name, the second was renamed to be
unique. That is only correct when the SQL references the parameter positionally ({0}), since
EF then writes the new name into the SQL. If the SQL hard-codes the name (e.g. "@Date"), it
kept pointing at the first parameter, so every subquery silently used the first value.

Now an InvalidOperationException is thrown when a DbParameter would need renaming but the SQL
doesn't reference its argument positionally. Parameters are not treated as interchangeable
even when their values are equal. Users should give each parameter a distinct name or use
positional placeholders.

This is a breaking change.

Fixes dotnet#32932

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@ajcvickers
ajcvickers force-pushed the fix-32932-fromsql-duplicate-dbparameter-names branch from 247997d to 0187017 Compare November 14, 2026 11:44
Copilot AI balanced review requested due to automatic review settings November 14, 2026 11:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

…nother DbParameter hard-codes it too

The names of DbParameters whose SQL references them by name can't be changed, so
collect them before processing and have every other parameter (EF-generated ones
for captured variables, generated pN names, and DbParameters referenced
positionally) avoid them, whichever is visited first. Only two DbParameters that
both hard-code the same name still throw.

Also:
- move EF.CompileQuery/CompileAsyncQuery out of the loops in the compiled-query
  tests so the same compiled query really is executed twice
- add a positive SqlQueryRaw test, and tests for captured variables (in a Union
  and in a predicate) next to FromSql DbParameters of the same name
- the placeholder scanner no longer accepts whitespace before the index, matching
  string.Format

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 16, 2026 10:45

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Parameter value mixup

2 participants