【问题标题】:Entity framework database-first approach stored procedure with output parameter and result set具有输出参数和结果集的实体框架数据库优先方法存储过程
【发布时间】:2017-01-10 03:18:51
【问题描述】:

我在我的 ASP.NET 项目中为实体框架使用数据库优先方法。我有一个存储过程,它有一个输出参数@TotalCount,它使用SELECT 语句返回一个结果集。

当我在我的MyDbContext.edmx 文件中添加存储过程时,MyDbContext.cs 有一个函数返回一个为存储过程自动生成的 int(可能是输出参数)。

如何使用这种方法访问输出参数和结果集?

下面给出了存储过程sn-p。基本上我正在尝试在存储过程中进行分页。

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;

MyDbContext.cs 文件中

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

【问题讨论】:

标签: c# .net sql-server asp.net-mvc stored-procedures


【解决方案1】:

首先您需要更新您的 EDMX 模型,以便您的功能

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

应该是这样的

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

要刷新 EDMX 模型,请转到 hear

您可能需要选择“创建新的复杂类型”选项,而不是上面链接中建议的“更新”

然后你可以使用下面的代码来获取结果

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;

【讨论】:

    猜你喜欢
    • 2015-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-28
    相关资源
    最近更新 更多