必须使用dbcontext.Database.SqlQuery声明标量变量’@custid’?

我试图使用EF5从bcontext.Database.SqlQuery执行存储过程。

抛出一个错误must declare the scalar variable '@custid'

  var results = _MiscContext.Database.SqlQuery( "exec sp_GetStaff @custid", customerNumber).ToList(); 

如果customerNumber是工作人员,则SP返回1 ,否则返回empty行。

 ALTER PROCEDURE [dbo].[sp_GetStaff] @custid varchar(12) AS BEGIN SET NOCOUNT ON; SELECT 1 AS [C1] FROM [dbo].[Staff] with (nolock) WHERE [CUSTOMER_ID] = @custid END 

如何管理?

由于您使用的是命名参数,因此必须为要传递的参数指定匹配的名称。

 var results = _MiscContext.Database.SqlQuery( "exec sp_GetStaff @custid", new SqlParameter("custid", customerNumber)).ToList(); 

尝试

 var results = _MiscContext.Database.SqlQuery( "exec sp_GetStaff {0}", customerNumber).ToList();