[英]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.