简体   繁体   中英

Error calling stored procedure from Entity Framework

I'm receiving the following error:

Procedure or function 'dsp_DeleteAgreementPackage' expects parameter '@AgreementPackageID', which was not supplied.

Here's my code:

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

I've also tried replacing the last line with:

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

I get the same error both ways. I've verified that param1 does include the integer value and a parameter name of @AgreementPackageID . Any idea what I'm doing wrong?

I think you're not including any parameter inside provided SQL command string (the parameters are required instead of simply using stored procedure name). Try using ExecuteSqlCommand with given @AgreementPackageID parameter as given below:

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

Also you can remove @ sign from ParameterName if necessary:

param1.ParameterName = "AgreementPackageID";

Similar issues:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM