简体   繁体   中英

The parameterized query is not recognizing parameter in EF5?

I am trying to execute a stored procedure from EF5 using Database.SqlQuery . But 2nd parameter is not recognizing here.

Error: "The parameterized query '(@custNum nvarchar(7), @PrimaryDisc bigint, @SecondaryDisc bigint)' expects the parameter '@PrimaryDisc', which was not supplied."

Code

var results = _MiscContext.Database.SqlQuery<TempTechDisciplines>(
        "exec sp_getTechnicalDiscipline @CustNum, @PrimaryDisc, @SecondaryDisc", 
        new SqlParameter("custNum", CustomerNum), 
        new SqlParameter("PrimaryDisc",SqlDbType.BigInt, 0), 
        new SqlParameter("SecondaryDisc",SqlDbType.BigInt, 0))
        .ToList<TempTechDisciplines>();

What is the issue here?

It works for me from this article.

http://blogs.msdn.com/b/diego/archive/2012/01/10/how-to-execute-stored-procedures-sqlquery-in-the-dbcontext-api.aspx

var custNum = new SqlParameter {ParameterName = "CustNum", Value = CustomerNum};
var primaryDisc = new SqlParameter { ParameterName = "PrimaryDisc", Value = 0 };
var secondaryDisc = new SqlParameter { ParameterName = "SecondaryDisc", Value = 0 };

var results = _MiscContext.Database.SqlQuery<TempTechDisciplines>(
             "exec sp_getTechnicalDiscipline @CustNum, @PrimaryDisc,
              @SecondaryDisc",
              custNum,primaryDisc,secondaryDisc).ToList<TempTechDisciplines>();

If a parameter value ( CustomerNum ) is null, the parameter will not be serialized, and this error will occur.

Check and add default values for each parameter you add ( '0' for CustomerNum ), and check if CustomerNum <> 0 instead IS NULL if needed.

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