EF4.1 代码优先:带有输出参数的存储过程 [英] EF4.1 Code First: Stored Procedure with output parameter
问题描述
我首先使用实体框架 4.1 代码.我想调用一个具有输出参数的存储过程,并检索该输出参数的值以及强类型结果集.它是一个带有这样签名的搜索功能
I use Entity Framework 4.1 Code First. I want to call a stored procedure that has an output parameter and retrieve the value of that output parameter in addition to the strongly typed result set. Its a search function with a signature like this
public IEnumerable<MyType> Search(int maxRows, out int totalRows, string searchTerm) { ... }
我发现了很多关于函数导入"的提示,但这与 Code First 不兼容.我可以使用 Database.SqlQuery(...) 调用存储过程,但这不适用于输出参数.
I found lots of hints to "Function Imports" but that is not compatible with Code First. I can call stored procedures using Database.SqlQuery(...) but that does not work with output parameters.
我可以使用 EF4.1 Code First 解决这个问题吗?
Can I solve that problem using EF4.1 Code First at all?
推荐答案
SqlQuery
使用输出参数,但您必须正确定义 SQL 查询和设置 SqlParameter
s.尝试类似:
SqlQuery
works with output parameters but you must correctly define SQL query and setup SqlParameter
s. Try something like:
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;
这篇关于EF4.1 代码优先:带有输出参数的存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!