asp.net-core-2.0 - 必须声明表变量“@p0”
问题描述
我正在使用ExecuteSqlCommand
实体框架核心来删除表的所有行。但是,我收到一个错误System.Data.SqlClient.SqlException: 'Must declare the table variable "@p0".'
这是我的代码:
public static void ExecuteDeleteSQL(ShoppingCartDbContext context, string tableName)
{
context.Database.ExecuteSqlCommand($"Delete from {tableName}"); //This line throws the error
}
它在这里被调用:
ExecuteDeleteSQL(context, "[dbo].[ShoppingCartItems]");
我正在使用 .NET Core 2 和 EntityFrameworkCore
我已经安装了以下 nuget 包:
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="2.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.0" />
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" />
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
解决方案
ExecuteSqlCommand
即使我的查询不使用任何参数,您似乎也必须至少传递一个参数。烦人的一点是它在编译时不会抱怨。
//passing at least one parameter
context.Database.ExecuteSqlCommand($"Delete from {tableName}",tableName);
编辑:
GitHub中报告了一个类似的问题,目前正在考虑中: https ://github.com/aspnet/EntityFrameworkCore/issues/10956
推荐阅读
- typescript - 如何在 Typescript 中传输类型
- java - Java无法从文件加载自定义字体并将JTextArea的字体设置为它
- elasticsearch - Elasticsearch - 计算重复值和唯一值
- python - 从文本文件中提取 JSON 密钥,然后创建 HTTP 请求
- c - 动态读取文件,不要在 C 中使用不必要的内存
- android-emulator - adb -s
壳下午清除 - 从模拟器中删除应用程序 - dart - Dart:如何截断字符串并在字符编号后添加省略号
- python - LabelImg imgSize 数组包含 0
- node.js - $match mongodb聚合框架_mongoose中的多条件
- python - Keras——使用 LSTM 层精度低,但没有 LSTM 时精度很好