如何首先通过EF代码从存储过程中检索输出参数

Mon*_*kar 12 c# stored-procedures entity-framework

我是EF的新手,并且首先使用EF代码.只是有一个链接https://code.msdn.microsoft.com/How-to-retrieve-output-e85526ba,它显示了如何首先使用EF db的读输出类型参数.所以有人告诉我如何首先通过EF代码从存储过程中检索输出参数?

如果可能的话,请给我一些小样本代码或将我重定向到相关文章.

谢谢

我有一个解决方案

var outParam = new SqlParameter();
outParam.ParameterName = "TotalRows";
outParam.SqlDbType = SqlDbType.Int;
outParam.ParameterDirection = ParameterDirection.Output;

var data = dbContext.Database.SqlQuery<MyType>("sp_search @SearchTerm, @MaxRows, @TotalRows OUT", 
               new SqlParameter("SearchTerm", searchTerm), 
               new SqlParameter("MaxRows", maxRows),
               outParam);
var result = data.ToList();
totalRows = (int)outParam.Value;
Run Code Online (Sandbox Code Playgroud)

Mon*_*nah 5

要检索存储过程调用的数据,可以使用以下命令

using(var db = new YourConext())
{
       var details = db.Database.SqlQuery<YourType>("exec YourProc @p", 
                      new SqlParameter("@p", YourValue));
}
Run Code Online (Sandbox Code Playgroud)

YourType:可能是int或string或long,甚至是ComplexType

@p:如果存储过程有参数,您可以根据需要从参数中定义

如果您需要有关SqlQuery的更多信息,可以查看以下内容

  1. 为实体编写SQL查询
  2. 实体框架代码优先和存储过程

希望对你有帮助

  • 这没有显示如何检索输出参数中的结果值。 (3认同)