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

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

问题描述

我有一个包含许多复杂存储过程的现有数据库,我想通过 EF 4 使用这些过程.我已完成以下操作:

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. 创建了一个 EF 数据对象,Customer.
  2. 在 EF 中添加了一个存储过程
  3. 右键单击 EF 设计器并添加函数导入.
  4. 函数导入名称 - MyFunction,复杂类型.
  1. Created an EF data object, 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);

现在我的存储过程有一个输出参数,我曾经通过引用(在 WebForm 中)调用它.但我收到以下错误:

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:

无法从引用字符串"转换为'System.Data.Objects.ObjectParameter'

cannot convert from 'ref string' to 'System.Data.Objects.ObjectParameter'

请帮忙

编辑

当我尝试保存时出现以下错误

When I am trying to save I am getting the below error

映射函数绑定指定函数 Model.Store.P_GetCustomer 和不受支持的参数:o_MyString.输出参数只能通过 RowsAffectedParameter 属性进行映射.使用结果绑定从函数调用返回值.

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.

推荐答案

输出参数在 ObjectParameter 实例中返回.所以你必须使用如下代码:

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();

原因是函数导入不能使用ref参数,因为直到你从数据库中处理结果集才填充输出参数=如果你不调用ToList或迭代存储过程的结果输出参数为空.

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天全站免登陆