EF中存储过程中的输出参数 [英] Output parameter in stored procedure in EF
问题描述
- 创建了一个EF数据对象。 [名称客户]
- 在EF中添加了一个存储过程
- 右键单击EF设计器并添加一个函数导入。
-
功能导入名称 - MyFunction,复杂类型。
CustomerEntities entity = new CustomerEntities();
var result = entity.MyFunction(XYZ,ref o_MyString);
现在我的存储过程有一个输出参数,我通过ref [在webform]中调用。但是我收到以下错误:
无法从'ref string'转换为
'System.Data.Objects.ObjectParameter '
请帮助
修改 / p>
当我尝试保存时,我收到以下错误
映射函数绑定指定具有不支持参数的函数Model.Store.P_GetCustomer:o_MyString。输出参数只能通过RowsAffectedParameter属性进行映射。使用结果绑定从函数调用返回值。
输出参数在 ObjectParameter
实例。所以你必须使用如下代码:
var oMyString = new ObjectParameter(o_MyString,typeof(string));
var result = ctx.MyFunction(XYZ,oMyString).ToList();
var data = oMyString.Value.ToString();
原因是函数导入不能使用ref参数,因为在处理结果集之前没有填充输出参数从数据库=如果不调用 ToList
或迭代存储过程的结果,输出参数为null。
I have an existing database with lots of complex stored procedure and I want to use those procedure through EF 4. I have done the following:
- Created an EF data object. [Name Customer]
- Added a Stored Procedure into the EF
- Right Click on the EF designer and add a function import.
Function Import Name - MyFunction, complex type.
CustomerEntities entity = new CustomerEntities(); var result = entity.MyFunction("XYZ", ref o_MyString);
Now my stored procedure has an output parameter which I used to call by the ref [in webform]. But I am getting the below error:
cannot convert from 'ref string' to 'System.Data.Objects.ObjectParameter'
Please help
Edit
When I am trying to save I am getting the below error
A mapping function binding specifies a function Model.Store.P_GetCustomer with an unsupported parameter: o_MyString. Output parameters may only be mapped through the RowsAffectedParameter property. Use result bindings to return values from a function invocation.
Output parameters are returned in ObjectParameter
instance. So you must use code like:
var oMyString = new ObjectParameter("o_MyString", typeof(string));
var result = ctx.MyFunction("XYZ", oMyString).ToList();
var data = oMyString.Value.ToString();
The reason is that function import cannot use ref parameter because output parameter is not filled until you process result set from the database = if you don't call ToList
or iterate the result of the stored procedure the output parameter is null.
这篇关于EF中存储过程中的输出参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!