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

时间:2021-10-26 22:35:12

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. 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行。但是,在插入,删除或其他调用中,我们需要此返回值来了解是否存在问题。我一直无法找到获得此返回值的方法。我可以得到输出值,我可以得到结果集,但没有返回值。

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!

谢谢您的帮助!

5 个解决方案

#1


2  

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。

#2


5  

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:

我想对存储过程返回值的支持取决于Entity框架的版本。虽然直接返回值对我不起作用,但我设法使用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

本文提供了有关从SP返回值的两种方法的一般信息

Hope this helps

希望这可以帮助

#3


3  

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版本以及你如何进行映射;如果你考虑在这里使用代码优先方法,你可以找到我的帖子与解决方案:

Get return value from stored procedure

从存储过程中获取返回值

#4


0  

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

#5


-1  

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

#1


2  

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。

#2


5  

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:

我想对存储过程返回值的支持取决于Entity框架的版本。虽然直接返回值对我不起作用,但我设法使用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

本文提供了有关从SP返回值的两种方法的一般信息

Hope this helps

希望这可以帮助

#3


3  

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版本以及你如何进行映射;如果你考虑在这里使用代码优先方法,你可以找到我的帖子与解决方案:

Get return value from stored procedure

从存储过程中获取返回值

#4


0  

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

#5


-1  

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