EF中存储过程中的输出参数 [英] Output parameter in stored procedure in EF

查看:263
本文介绍了EF中存储过程中的输出参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的数据库,有很多复杂的存储过程,我想通过EF 4使用这些过程。我做了以下工作:


  1. 创建了一个EF数据对象。 [名称客户]

  2. 在EF中添加了一个存储过程

  3. 右键单击EF设计器并添加一个函数导入。

  4. 功能导入名称 - 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:

  1. Created an EF data object. [Name Customer]
  2. Added a Stored Procedure into the EF
  3. Right Click on the EF designer and add a function import.
  4. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆