C# 实体框架 - 存储过程返回值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10339750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Entity Framework - stored procedure return value
提问by bugnuker
I am trying to get the return value of a stored procedure. Here is an example of such a stored procedure:
我正在尝试获取存储过程的返回值。以下是此类存储过程的示例:
select
Name,
IsEnabled
from
dbo.something
where
ID = @ID
if @@rowcount = 0
return 1
return
This is a simple select. If 0 rows are found, my result set will be null, but I will still have a return value.
这是一个简单的选择。如果找到 0 行,我的结果集将为空,但我仍然会有返回值。
This is a bad example, as this is a select, so sure I could find if 0 rows were returned. However, on an Insert, delete, or other calls, we need this return value to know if there was a problem. I have been unable to find a way to get this return value. I can get output values, I can get result sets, but no return value.
这是一个不好的例子,因为这是一个选择,所以我可以确定是否返回了 0 行。但是,在 Insert、delete 或其他调用中,我们需要此返回值来了解是否存在问题。我一直无法找到获得此返回值的方法。我可以获得输出值,我可以获得结果集,但没有返回值。
I can get the return value if I call SQL manually, or even if I run a SqlCommandusing the Entity Framework, but this is not what I want to do.
如果我手动调用 SQL,或者即使我SqlCommand使用实体框架运行 a,我也可以获得返回值,但这不是我想要做的。
Has anyone ever been able to get the return value from a stored procedure using Entity Framework?
有没有人能够使用实体框架从存储过程中获取返回值?
Thanks for the help!
谢谢您的帮助!
采纳答案by jrummell
No. Entity Framework doesn't have rich stored procedure support because its an ORM, not a SQL replacement.
否。实体框架没有丰富的存储过程支持,因为它是一个 ORM,而不是 SQL 替代品。
As you have already discovered, if you need to use less common (more advanced?) features of stored procedures, you'll have to use good old fashioned ADO.NET.
正如您已经发现的那样,如果您需要使用存储过程的不太常见(更高级?)的功能,则必须使用良好的老式 ADO.NET。
回答by Mehrdad
In order to get the interested result in EF, you should return a same structure (not 1 and Name, IsEnabled) In this query you should return '',0 for example instead of 1 in the if condition:
为了在 EF 中获得感兴趣的结果,您应该返回相同的结构(不是 1 和 Name,IsEnabled)在此查询中,您应该返回 '',0 例如,而不是 if 条件中的 1:
select
Name,
IsEnabled
from
dbo.something
where
ID = @ID
if @@rowcount = 0
return '',0
回答by Daniele Armanasco
I have been able to get the return value (and also output and input parameters) with Entity Framework 5 (but I think it works from version 4.x) using the code first approach. From your post I don't see what version of EF you are using and how you're doing your mapping; if you consider to use code-first approach here you can find my post with the solution:
我已经能够使用代码优先方法通过 Entity Framework 5(但我认为它从 4.x 版开始工作)获得返回值(以及输出和输入参数)。从您的帖子中,我看不到您使用的是哪个版本的 EF 以及您如何进行映射;如果您考虑在此处使用代码优先方法,您可以在我的帖子中找到解决方案:
回答by AlexT
I guess support of stored procedure return values depends on version of Entity framework. Although directly returning value didn't work for me I managed to get value using OUTPUT stored procedure parameter. Example:
我猜对存储过程返回值的支持取决于实体框架的版本。尽管直接返回值对我不起作用,但我设法使用 OUTPUT 存储过程参数获取了值。例子:
USE [YOUR_DB_NAME]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[TestReturnValue]
@InputValue int = 0,
@Result int OUTPUT
AS
BEGIN
SET NOCOUNT ON;
SELECT @Result = @InputValue
RETURN(@InputValue);
END
This is test code:
这是测试代码:
void TestReturnValue()
{
using (var db = new YourDBContext())
{
var result = new System.Data.Objects.ObjectParameter("Result", 0);
Console.WriteLine("SP returned {0}", db.TestReturnValue(123, result));
Console.WriteLine("Output param {0}", result.Value);
}
}
And this is output:
这是输出:
SP returned -1
Output param 123
As you see directly returned value output some rubbish but OUTPUT parameters works!
如您所见,直接返回值输出一些垃圾,但 OUTPUT 参数有效!
This article provides general info on two ways of returning values from SP
Hope this helps
希望这可以帮助
回答by Hadi Salehy
You must use the "Context.Database.SqlQuery",and specify the output type of the stored procedure
必须使用“Context.Database.SqlQuery”,并指定存储过程的输出类型
var spReturnVaue = Context.Database.SqlQuery<Type>("[schema].YourSp").FirstOrDefault();
int firstId = Context.Database.SqlQuery<int>("[dbo].GetFirstId").FirstOrDefault();

