简体   繁体   English

具有输出参数和结果集的实体框架数据库优先方法存储过程

[英]Entity framework database-first approach stored procedure with output parameter and result set

I am using database-first approach for Entity Framework in my ASP.NET project. 我在ASP.NET项目中对实体框架使用数据库优先方法。 I have a stored procedure that has an output parameter @TotalCount and it returns a result set using a SELECT statement. 我有一个具有输出参数@TotalCount的存储过程,它使用SELECT语句返回结果集。

When I add the stored procedure in my MyDbContext.edmx file, the MyDbContext.cs has a function that returns an int (probably the output parameter) that is automatically generated for the stored procedure. 当我在MyDbContext.edmx文件中添加存储过程时, MyDbContext.cs具有一个函数,该函数返回为存储过程自动生成的int(可能是输出参数)。

How can I access both the output parameter as well as the result set with this approach? 如何使用这种方法访问输出参数和结果集?

The stored procedure snippet is given below. 存储过程代码段如下所示。 Basically I am trying to do pagination in the stored procedure. 基本上,我正在尝试在存储过程中进行分页。

CREATE PROCEDURE [dbo].[sp_GetDetailsForStudent]
    @StudentId BIGINT,
    //....
    @OrderBy NVARCHAR(max),
    @Page INT OUTPUT,
    @Items INT = 200,
    @TotalCount INT OUTPUT
    //.....

    SET @SortSql = N'SELECT * FROM #tmpTable'
    //......    
    EXEC sp_executesql @SortSql;

In the MyDbContext.cs file MyDbContext.cs文件中

 public virtual int sp_GetDetailsForStudent(parameters ...){
     //......
     return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("sp_GetDetailsForStudent", input parameters..., totalCount);
 }

First you need to update your EDMX model so that your function 首先,您需要更新EDMX模型,以便您的功能

public virtual int sp_GetDetailsForStudent(parameters ...){....}

Should look like 应该看起来像

public virtual ObjectResult<YourResult_type> sp_GetDetailsForStudent(parameters ...){....}

For refreshing EDMX model go hear 要刷新EDMX模型,请

You might need to choose "Create New Complex Type" option instead of "Update" as suggested in above link. 您可能需要选择“创建新的复杂类型”选项,而不是上面的链接中建议的“更新”选项。

Then to fetch the result you can use below code 然后要获取结果,可以使用以下代码

ObjectParameter outParam1 = new ObjectParameter("outParameter", typeof(long));
var db = new YourDbContext();
var res = db.sp_GetDetailsForStudent(parameter1,parameter2,outParam1 );
foreach (YourResult_type item in res)
{
    var prop1= item.property1;
}
var outParam1Value= outParam1.Value;

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

相关问题 在Entity Framework数据库优先方法中,如何从存储过程返回多个结果集? - In Entity Framework database-first approach how to return multiple result sets from a stored procedure? 实体框架数据库中的存储过程第一种方法 - Stored procedure in Entity Framework database first approach Oracle的存储过程不适用于Entity Framework数据库优先的工作流程 - Stored procedure from Oracle doesn't work with Entity Framework database-first workflow 实体框架中没有自动生成代码的数据库优先方法 - Database-first approach in Entity Framework without auto generated code 数据库优先方法中的Linq-to-SQL与实体框架 - Linq-to-SQL vs Entity Framework in database-first approach 在“实体框架”数据库优先方法中,将“默认值1”设置为EntityName(状态) - Set Default Value 1 to an EntityName (Status) in Entity Framework database-first approach 实体框架数据库优先方法有条件地插入数据 - Entity Framework database-first approach conditionally insert data 使用参数和多个结果集实体框架调用存储过程6 - Calling Stored Procedure with Parameter and Multiple Result Set Entity Framework 6 如何首先从实体框架+数据库中的存储过程中获取结果 - how to get the result from stored procedure in entity framework + database first 实体框架数据库首先将参数添加到存储过程 - Entity Framework Database first add parameter to stored procedure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM