Repository navigation
Throw when same-named DbParameters are hard-coded in FromSql/SqlQuery SQL - #39593
Open
ajcvickers wants to merge 2 commits into
Open
ajcvickers wants to merge 2 commits into
ajcvickers wants to merge 2 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>
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
RelationalParameterProcessornow throws anInvalidOperationExceptionwhen aDbParameterwould need renaming but the SQL doesn't reference its argument positionally ({n},{n,w}or{n:fmt}; escaped{{n}}doesn't count). 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. This also covers a hard-coded name that collides with an EF-generated one (p0).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 this accidentally working (same name, same value, hard-coded) now throw, with a message explaining the fix (distinct names, or positional placeholders). Targeting
mainonly; 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 (works), positional then hard-coded (throws), prefixed vs unprefixed, case differences, collision with a generated name, compiled queries (throws on every execution; positional works on repeat execution), andSqlQueryRaw.{00}, escaped braces,{0,5},{0:x}, etc.).Ran SQL Server and SQLite functional tests and the full
test.cmd. Three unrelated failures in the parallel run (two ManyToMany tests and oneMicrosoft.Data.Sqlitebusy-commit test) pass when re-run on their own.🤖 Generated with Claude Code