Skip to content

SqlParameter doesn't work for FromSqlRaw #4919

@coin8086

Description

@coin8086

Type of issue

Code doesn't work

Description

The sample code for FromSqlRaw doesn't work.

var columnName = "Url";
var columnValue = new SqlParameter("columnValue", "http://SomeURL");

var blogs = context.Blogs
    .FromSqlRaw($"SELECT * FROM [Blogs] WHERE {columnName} = @columnValue", columnValue)
    .ToList();

It produces a runtime error

System.InvalidCastException: Unable to cast object of type 'Microsoft.Data.SqlClient.SqlParameter' to type 'Microsoft.Data.Sqlite.SqliteParameter'.

Here the type SqlParameter is incorrect. It should be a provider specific type, say SqliteParameter for SQLite, NpgsqlParameter for Npgsql.

Page URL

https://learn.microsoft.com/en-us/ef/core/querying/sql-queries?tabs=sqlserver

Content source URL

https://github.com/dotnet/EntityFramework.Docs/blob/main/entity-framework/core/querying/sql-queries.md

Document Version Independent Id

98b01ef5-24a5-5f59-30c9-af264f739815

Article author

@smitpatel

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions