简体   繁体   English

对于实体框架中的所有情况,存储过程返回-1

[英]Stored Procedure return -1 for all cases in entity framework

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. 我已创建此Stored Procedure并使用实体框架调用此Stored Procedure Below is code in written in C#. 下面是用C#编写的代码。

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

spIsValidUser Stored Procedure return -1 in all case. spIsValidUser Stored Procedure在所有情况下都返回-1 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 ? 编辑 - 根据给定的答案,存储过程不使用return语句,因为实体框架不能支持存储过程返回开箱即用的标量值。让我知道如何从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): 像这样更改你的存储过程(不要在存储过程中使用关键字return来返回值,Entity Framework不能支持存储过程返回开箱即用的量值。但是有一个解决方法):

     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 . 您需要将存储过程作为Function导入。 Right-click on the workspace area of your Entity model and choose Add -> Function Import . 右键单击Entity模型的工作区区域,然后选择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 . 在“ Add Function Import对话框中,输入要在模型中引用存储过程的名称,从下拉列表中选择过程,然后选择过程的返回值为标量

  • 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. 实体框架也没有丰富的存储过程支持,因为它是一个ORM,而不是SQL替代品。

Have you imported your stored procedures in the EF model correctly? 您是否正确地在EF模型中导入了存储过程? and are you setting correct return type to stored procedures?? 你正在为存储过程设置正确的返回类型吗?

There are 4 possible return type that you can set to your procedures The possible return types are: 您可以为您的过程设置4种可能的返回类型可能的返回类型是:

  1. none 没有
  2. Scalars 标量
  3. Complex 复杂
  4. Entities 实体

you need to set scalars return type. 你需要设置标量返回类型。

if you dont know how to set the return type, then here is the full tutorial http://www.binaryintellect.net/articles/30738a7c-5176-4333-aa83-98eab8548da5.aspx 如果您不知道如何设置返回类型,那么这里是完整的教程http://www.binaryintellect.net/articles/30738a7c-5176-4333-aa83-98eab8548da5.aspx

quick example. 快速举例。

CREATE PROCEDURE CustOrderCount
    @CustomerID nchar(5)
AS
BEGIN
    SELECT COUNT(*) FROM ORDERS 
        WHERE CUSTOMERID=@CustomerID;
END


NorthwindEntities db = new NorthwindEntities();
var count = db.CustOrderCount("ALFKI");
int ordercount = count.SingleOrDefault().Value;

this will return int order count. 这将返回int order count。

Have you tried: 你有没有尝试过:

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

    END
    ELSE
    BEGIN
        SELECT 1
    END
 GO

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

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