Repository navigation
Throw when same-named DbParameters are hard-coded in FromSql/SqlQuery SQL - #39593
Open
ajcvickers wants to merge 3 commits into
Open
ajcvickers wants to merge 3 commits into
ajcvickers wants to merge 3 commits into
Conversation
… 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
force-pushed
the
fix-32932-fromsql-duplicate-dbparameter-names
branch
from
November 14, 2026 11:44
247997d to
0187017
Compare
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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 exampleSELECT @date AS Date), it kept pointing at the first parameter, so every subquery silently used the first value.Change
A
DbParameterwhose name is written directly into the SQL passed toFromSqlRaw/SqlQueryRawcan't be renamed.RelationalParameterProcessornow collects those names before it processes the query, and every other parameter avoids them, whichever is encountered first:DbParameterreferenced positionally ({n},{n,w}or{n:fmt}; escaped{{n}}doesn't count) is renamed if its name is taken;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 anInvalidOperationException, 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 sameDbParameterinstance (#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 positionalDbParameter, no longer causes a problem: EF renames its own. Targetingmainonly; not for servicing. Needs thebreaking-changelabel. Docs PR: dotnet/EntityFramework.Docs#5516Tests
FromSqlQueryTestBase/SqlQueryTestBasetests (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 theDbParameter(in aUnionand in a predicate; works), compiled queries (the same compiled query throws on every execution; positional works on repeat execution), andSqlQueryRaw.{00}, escaped braces,{0,5},{0:x}, whitespace before the index, etc.).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.cmdfor the first two commits. Three unrelated failures in the parallel run (two ManyToMany tests and oneMicrosoft.Data.Sqlitebusy-commit test) pass when re-run on their own. The follow-up commit was verified with the SQL Server and SQLiteFromSqlQuery/SqlQuerytest classes and theRelationalParameterProcessorunit tests.🤖 Generated with Claude Code