I have seen this thread: How to pass parameters to the DbContext.Database.ExecuteSqlCommand method?
I have a feeling EF Core is preventing me from using a Parameterized query to truncate
tables in an Azure Sql Database from working.
I have tried:
var tableName = csvEntity.FileName.Replace(".csv", string.Empty);
var tableNameParam = new SqlParameter("@TableName", tableName);
await DbContext.Database.ExecuteSqlCommandAsync("TRUNCATE TABLE @TableName", tableNameParam);
And:
await DbContext.Database.ExecuteSqlCommandAsync("TRUNCATE TABLE @{0}", tableNameParam);
And:
await DbContext.Database.ExecuteSqlCommandAsync($"TRUNCATE TABLE {tableName}");
And:
await DbContext.Database.ExecuteSqlCommandAsync("TRUNCATE TABLE {tableName}", tableName);
But all result in some variation of:
Error: Incorrect syntax near '@TableName'.
BUT if I run
await DbContext.Database.ExecuteSqlCommandAsync("TRUNCATE TABLE AllStarFull");
We're all good!
Can you not use a variable as a table name in a truncate statement with ExecuteSqlCommandAsync
Some screen shots:
Exception:
3rd Attempt Before Exception
3rd Attempt Exception