从Entity Framework调用存储过程时出错

时间:2017-08-31 01:17:41

标签: c# entity-framework stored-procedures

我收到以下错误:

  

过程或函数'dsp_DeleteAgreementPackage'需要参数'@AgreementPackageID',这是未提供的。

这是我的代码:

var param1 = new SqlParameter();
param1.ParameterName = "@AgreementPackageID";
param1.SqlDbType = SqlDbType.Int;
param1.SqlValue = package.Id;
_context.Database.ExecuteSqlCommand("dsp_DeleteAgreementPackage", param1);

我也尝试用以下代码替换最后一行:

_context.Database.ExecuteSqlCommand("dsp_DeleteAgreementPackage", new object[] { param1 });

我两种方式都得到同样的错误。我已验证param1确实包含整数值和参数名称@AgreementPackageID。知道我做错了吗?

1 个答案:

答案 0 :(得分:1)

我认为你没有在提供的SQL命令字符串中包含任何参数(需要参数而不是简单地使用存储过程名称)。尝试将ExecuteSqlCommand与给定的@AgreementPackageID参数一起使用,如下所示:

_context.Database.ExecuteSqlCommand("EXEC dsp_DeleteAgreementPackage @AgreementPackageID", param1);

如果需要,您也可以从@删除ParameterName个符号:

param1.ParameterName = "AgreementPackageID";

类似问题:

EntityFramework Procedure or function '' expects parameter '', which was not supplied

Call stored procedure using ExecuteSqlCommand (expects parameters which was not supplied)