存储过程对于实体框架中的所有情况都返回-1 [英] Stored Procedure return -1 for all cases in entity framework

查看:71
本文介绍了存储过程对于实体框架中的所有情况都返回-1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  CREATE PROC spIsValidUser 
@UserName varchar(50),
@Password varchar(50)
AS
如果存在(SELECT * FROM用户,其中UserName = @ UserName和Password = @ Password)
BEGIN
返回0

END
ELSE
BEGIN
返回1
END
GO

我创建了这个存储过程并使用实体框架调用这个存储过程。下面是用C#编写的代码。

  MyBusEntities db = new MyBusEntities(); 
int empQuery = db.spIsValidUser(abc,abc @ 123);

spIsValidUser 存储程序返回 -1 在所有情况下。请让我知道错误。



编辑 - 根据给定的答案,存储过程不使用return语句,因为实体框架不支持存储过程返回标准值开箱即可。让我知道如何从存储过程发送标量数据?

解决方案

您的存储过程当前返回一个标量值。使用以下步骤来解决此问题:




  • 更改您的存储过程(不要使用关键字 return 在存储过程中返回值,Entity Framework不支持存储过程返回值开箱即用,但有一个工作):

      ALTER PROC spIsValidUser 
    @UserName varchar(50),
    @Password varchar(50)
    AS
    SELECT Count(*)FROM UserName = @UserName和Password = @Password的用户
    返回


  • 您需要将存储过程作为函数导入。右键单击实体模型的工作区,然后选择 Add - >功能导入


  • 添加功能导入对话框中,输入名称,您希望在模型中引用您的存储过程,从下拉列表中选择您的过程,并选择该过程的返回值为 Scalar 。 / p>


  • 最后写你这样的代码:

      MyBusEntities db = new MyBusEntities(); 
    System.Nullable< int> empQuery = db.spIsValidUser(abc,abc @ 123)。SingleOrDefault()。
    MessageBox.Show(empQuery.ToString()); //显示1如果Exist和0如果不存在




编辑:我认为对存储过程返回值的支持取决于Entity框架的版本。另外,实体框架没有丰富的存储过程支持,因为它的ORM不是SQL替换。


CREATE PROC spIsValidUser
     @UserName varchar(50),
     @Password varchar(50) 
AS
    IF  Exists(SELECT * FROM Users where UserName=@UserName and Password=@Password)
    BEGIN
        return 0

    END
    ELSE
    BEGIN
        return 1
    END
 GO

I have created this Stored Procedure and tring to call this Stored Procedure using entity framework. Below is code in written in C#.

MyBusEntities db = new MyBusEntities();
int empQuery = db.spIsValidUser("abc", "abc@123");

spIsValidUser Stored Procedure return -1 in all case. Please let me know error.

EDIT - According to given answer, Store procedure is not used return statement because Entity Framework cannot support Stored Procedure Return scalar values out of the box..Let me know how can I send scalar data from Stored Procedure?

解决方案

Your stored procedure is currently returns a scalar value. Use the following steps to solve this issue:

  • Change your stored procedure like this (Don't use the keyword return in the stored procedure to return the value, Entity Framework cannot support Stored Procedure Return scalar values out of the box. BUT there is a work around):

    ALTER PROC spIsValidUser
    @UserName varchar(50),
    @Password varchar(50) 
    AS
    SELECT Count(*) FROM Users where UserName= @UserName and Password= @Password
    return
    

  • You need to Import the stored procedure as a Function. Right-click on the workspace area of your Entity model and choose Add -> Function Import.

  • In the Add Function Import dialog, enter the name you want your stored procedure to be referred to in your model, choose your procedure from the drop down list, and choose the return value of the procedure to be Scalar.

  • Finally write you code like this:

    MyBusEntities db = new MyBusEntities();
    System.Nullable<int> empQuery = db.spIsValidUser("abc", "abc@123").SingleOrDefault().Value;
    MessageBox.Show(empQuery.ToString());// show 1 if Exist and 0 if not Exist
    

Edit: I think support of stored procedure return values depends on version of Entity framework. Also Entity Framework doesn't have rich stored procedure support because its an ORM, not a SQL replacement.

这篇关于存储过程对于实体框架中的所有情况都返回-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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