question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ExecuteSqlInterpolated and ExecuteSqlInterpolatedAsync do not work with variable schema, table and property name

See original GitHub issue

The 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:

Invalid object name ‘@p0.@p1’.

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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
ajcvickerscommented, Oct 15, 2019

@TanvirArjel Not sure exactly what you mean. It’s limited by what T-SQL on SQL Server supports.

3reactions
Shane32commented, Oct 7, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found