简体   繁体   English

实体框架 - 存储过程返回值

[英]Entity Framework - stored procedure return value

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行,我的结果集将为null,但我仍然会有一个返回值。

This is a bad example, as this is a select, so sure I could find if 0 rows were returned. 这是一个糟糕的例子,因为这是一个选择,所以我确定我能找到返回0行。 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. 我可以得到输出值,我可以得到结果集,但没有返回值。

I can get the return value if I call SQL manually, or even if I run a SqlCommand using the Entity Framework, but this is not what I want to do. 如果我手动调用SQL,或者即使我使用实体框架运行SqlCommand ,我也可以获得返回值,但这不是我想要做的。

Has anyone ever been able to get the return value from a stored procedure using Entity Framework? 有没有人能够使用Entity Framework从存储过程中获取返回值?

Thanks for the help! 谢谢您的帮助!

I guess support of stored procedure return values depends on version of Entity framework. 我想对存储过程返回值的支持取决于Entity框架的版本。 Although directly returning value didn't work for me I managed to get value using OUTPUT stored procedure parameter. 虽然直接返回值对我不起作用,但我设法使用OUTPUT存储过程参数获取值。 Example: 例:

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 本文提供了有关从SP返回值的两种方法的一般信息

Hope this helps 希望这可以帮助

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. 我已经能够使用代码第一种方法获得Entity Framework 5的返回值(以及输出和输入参数)(但我认为它适用于版本4.x)。 From your post I don't see what version of EF you are using and how you're doing your mapping; 从你的帖子我看不到你正在使用的EF版本以及你如何进行映射; if you consider to use code-first approach here you can find my post with the solution: 如果你考虑在这里使用代码优先方法,你可以找到我的帖子与解决方案:

Get return value from stored procedure 从存储过程中获取返回值

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。

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();

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)在这个查询中,你应该在if条件中返回'',0而不是1:

select
    Name,
    IsEnabled
from
    dbo.something
where
    ID = @ID

if @@rowcount = 0
    return '',0

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

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