我是 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;
最佳答案
要检索存储过程调用的数据,您可以使用以下命令
using(var db = new YourConext())
{
var details = db.Database.SqlQuery<YourType>("exec YourProc @p",
new SqlParameter("@p", YourValue));
}
YourType:可能是 int 或 string 或 long 甚至是 ComplexType
@p:如果存储过程有参数,你可以根据需要定义任意数量的参数
如果您需要更多关于 SqlQuery 的信息,您可以查看以下内容
希望对你有帮助
关于c# - 如何首先通过EF代码从存储过程中检索输出参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39527003/