必须使用dbcontext.Database.SqlQuery声明标量变量'@custid'? [英] must declare the scalar variable '@custid' using dbcontext.Database.SqlQuery?

查看:120
本文介绍了必须使用dbcontext.Database.SqlQuery声明标量变量'@custid'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to execute stored procedure from bcontext.Database.SqlQuery using EF5.

抛出错误必须声明标量变量'@custid'

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

如果customerNumber是staff,则SP返回 1 它返回 empty 行。

SP returns 1 if customerNumber is staff otherwise it return empty row.

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

如何管理?

推荐答案

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

Since you're using named parameters, you have to specify the matching name for the parameter you're passing.

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

这篇关于必须使用dbcontext.Database.SqlQuery声明标量变量'@custid'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆