参数化查询无法识别EF5中的参数?

时间:2013-11-26 18:20:57

标签: c# asp.net entity-framework

我正在尝试使用Database.SqlQuery从EF5执行存储过程。但是第二个参数在这里没有认识到。

  

错误:“参数化查询”(@custNum nvarchar(7),@ PopDisc bigint,@ SecaryDisc bigint)'需要参数'@PrimaryDisc',这是未提供的。“

代码

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>();

这里有什么问题?

1 个答案:

答案 0 :(得分:1)

这篇文章对我有用。

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>();