简体   繁体   English

实体框架从存储过程返回错误的值

[英]Entity Framework returns the wrong value from stored procedure

I have this SQL Server stored procedure: 我有这个SQL Server存储过程:

    @UserName VARCHAR(MAX),
    @Result INT
AS
BEGIN
    SET NOCOUNT ON;

    SELECT LoginID 
    FROM Personnel
    WHERE LoginID = @UserName AND Administrator = 'Y'

    SET @Result = @@ROWCOUNT

    RETURN @Result
END

When I test it I get the results back as expected, I get either 1 row or a 0 row count. 当我测试它时,我得到了预期的结果,我得到1行或0行计数。

I call the stored procedure from my C# application with this code: 我使用以下代码从我的C#应用​​程序调用存储过程:

private void ValidateUser(string user)
{
    using (ComplaintsEntities db = new ComplaintsEntities())
    {
        var t = db.AAGetAdminStatus(user,0);
    }
}

When I test with a good user account I should get a result of 1 and if my user is not admin I should get a result of 0 . 当我和良好的用户帐户测试我应该得到的结果是1 ,如果我的用户没有联系我应该得到的结果是0 When I inspect the return value in the C# code, it always returns -1 . 当我检查C#代码中的返回值时,它总是返回-1 How can I get this to work? 我怎样才能让它发挥作用?

@UserName varchar(max)
AS
BEGIN
  -- SET NOCOUNT ON added to prevent extra result sets from
  -- interfering with SELECT statements.
  SET NOCOUNT ON;

  -- Insert statements for procedure here
  Select count(LoginID) UserCount
  from Personnel
  where LoginID = @UserName And Administrator = 'Y'

END

Entity code: 实体代码:

private void ValidateUser(string user)
{
    using (ComplaintsEntities db = new ComplaintsEntities())
    {
        var t = db.AAGetAdminStatus(user).First().UserCount;
    }
}

Mostafa's answer kind of worked. 莫斯塔法的回答有点奏效。 I used his SQL code to return the count but had to modify the C# code a little. 我使用他的SQL代码返回计数但必须稍微修改C#代码。 Had to change it to. 不得不改变它。

var x = db.AAGetAdminStatus(user).First();

Once I did that I had what I needed. 一旦我这样做,我就得到了我需要的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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