存储过程返回-1为实体framwork所有病例 [英] Stored Procedure return -1 for all cases in entity framwork
问题描述
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
我创造了这个存储过程
和特林使用实体框架的存储过程来调用这个。 。下面是用C#编写代码
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
存储程序
收益 1
在所有的情况下。请让我知道误差
spIsValidUser
Stored Procedure
return -1
in all case. Please let me know error.
修改 - 怎么一回事,因为实体框架根据给出的答案,不使用存储过程返回语句不支持存储过程返回出box..Let的标量值我知道我可以发标量数据从存储过程
?
EDIT - According to given answer, Store procedure is not used return statement beacuse 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
编辑:我想支持存储过程返回值取决于实体框架的版本。还实体框架不具备丰富的存储过程的支持,因为它的ORM,而不是一个SQL替代品。
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为实体framwork所有病例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!