Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SqlQuery docs state to use parameter placeholders and supply additional parameters as arguments, but that's not the case for .SqlQuery #4957

Open
BrettBloomerang opened this issue Feb 26, 2025 · 2 comments

Comments

@BrettBloomerang
Copy link

Type of issue

Outdated article

Description

With current EFCore 9 (and I believe since 7?) the .SqlQuery method does not accept any arguments except for the query itself. I believe it's supposed to function like .FromSql, where interpolated strings are automatically parameterized, hence the existence of .SqlQueryRaw.

Either the documentation is outdated/incorrect, or this code is going to cause sql injection attacks to become more prevalent.

Page URL

https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.relationaldatabasefacadeextensions.sqlquery?view=efcore-9.0

Content source URL

https://github.com/dotnet/EntityFramework.ApiDocs/blob/live/dotnet/xml/Microsoft.EntityFrameworkCore/RelationalDatabaseFacadeExtensions.xml

Document Version Independent Id

731055e2-2c41-b2bf-4d08-8d06cd1d6cc5

Article author

@dotnet-bot

@roji
Copy link
Member

roji commented Mar 3, 2025

the .SqlQuery method does not accept any arguments except for the query itself.

SqlQuery() allows safe interpolation with parameterization, just like FromSql does:

var id = 8;
var names = await context.Database
    .SqlQuery<int>($"SELECT [Name] AS [Value] FROM [Blogs] WHERE [Id] > {id}")
    .ToListAsync();

Am I missing something?

@BrettBloomerang
Copy link
Author

@roji You and I agree on that. The documentation says otherwise, though. From the doc page:

You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter.

^That piece of the "remarks" section is untrue.

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

No branches or pull requests

2 participants