ExecuteSqlInterpolated and ExecuteSqlInterpolatedAsync do not work with variable schema, table and property name
See original GitHub issueThe following query does not work although interpolated sql string is valid:
await _dbContext.Database.ExecuteSqlInterpolatedAsync($"Delete From [{schema}].[{tableName}] Where {primaryKeyName} = {primaryKeyValue}"); // This does not work
and throws the following exception:
If I execute the above interpolated string with ExecuteSqlRaw
and ExecuteSqlRawAsync
as follows it works gracefully:
var deleteCommand = $"Delete From [{schema}].[{tableName}] Where {primaryKeyName} = {primaryKeyValue}";
await _dbContext.Database.ExecuteSqlRawAsync(deleteCommand); // This works
Am I missing anything in case of ExecuteSqlInterpolated
and ExecuteSqlInterpolatedAsync
and this is a just a critical bug?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Entity Framework Core ExecuteSqlInterpolated gives ...
The property Name looks like Person.Address where Person is the schema name under which the table is placed. The version of Entity Framework ......
Read more >SQL Queries - EF Core
This code doesn't work, since databases do not allow parameterizing column names (or any other part of the schema).
Read more >How to Execute Stored Procedures With EF Core 7
In this article, we are going to ;earn how to execute stored procedures in Entity Framework Core 7 with different examples.
Read more >Some Overlooked EF Core 7 Changes and Improvements
A new method of DbContext.Database, called SqlQuery , lets you pass in a raw SQL query to get scalar data directly from the...
Read more >Untitled
... 2019 · Insights New issue ExecuteSqlInterpolated and ExecuteSqlInterpolatedAsync do not work with variable schema, table and property … is cathole a ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@TanvirArjel Not sure exactly what you mean. It’s limited by what T-SQL on SQL Server supports.
ExecuteSqlInterpolatedAsync replaces variables with SQL parameters to prevent SQL injection attacks. So the executed SQL would look something like the following.
Delete FROM [@p0].[@p1] Where @p2 = @p3
Since SQL does not allow variables to contain member names like the schema, tableName, or primaryKeyName, the above SQL is invalid. You would need to use ExecuteSqlRawAsync for your purpose, embedding the schema, tableName, and primaryKeyName directly into the SQL as shown in your example. The primaryKeyValue could be a parameter.