原文地址:http://blogs.microsoft.co.il/gilf/2010/05/09/how-to-retrieve-stored-procedure-output-parameters-in-entity-framework/ 

How to Retrieve Stored Procedure Output Parameters in Entity Framework

One question that 
raises from time to 
time in EF forums 
is how you can 
retrieve stored 
procedure output 
parameters in EF 
Function Import. 
This post will show you how to do that.

The Stored Procedure

In the example I’m going to use the following stored procedure:

CREATE PROCEDURE dbo.SchoolBudgetForDateRange
    @StartDate DATETIME,
    @EndDate DATETIME,
    @Sum money output
AS
SET NOCOUNT ON;
SELECT @Sum = SUM(Department.Budget)
FROM Department 
WHERE StartDate BETWEEN @StartDate AND @EndDate

The stored procedure returns the school budget for a given date. 
By of course the stored procedure could return that calculation 
without using an output parameter.

Retrieving Stored Procedure Output Parameter

After creating a Function Import (which is explained here) we can 
use the SchoolBudgetforDateRange method with the context we have. 
In order to get an output parameter you need to supply an ObjectParameter 
to the stored procedure call which holds the parameter name and type. 
After the execution of the stored procedure you can retrieve the 
parameter using the Value property of the ObjectParameter
The following code shows how to that exactly what I wrote:

static void Main(string[] args)
{
  using (SchoolEntities context = new SchoolEntities())
  {
    var outputParameter = new ObjectParameter(“sum”, typeof(decimal));
    context.SchoolBudgetForDateRange(new DateTime(2007, 1, 1), 
      new DateTime(2008, 1, 1), 
      outputParameter);
    Console.WriteLine(outputParameter.Value);
  }
}

Summary

Once you need to retrieve output parameters from EF Function Imports
you need to supply an ObjectParameter to hold the output. In the post 
I showed how to do that.

----------------------------------------

自我测试

ALTER PROC [dbo].[usp_AddTeacherInfo]
@Name NVARCHAR(50),
@Phone NVARCHAR(50),
@Address NVARCHAR(50),
@Age INT,
@Sum UNIQUEIDENTIFIER OUTPUT
AS
BEGIN
SET NOCOUNT ON
DECLARE @TmpID UNIQUEIDENTIFIER
SET @TmpID = NEWID()
INSERT INTO dbo.Teacher
(ID, Name, Phone, Address, Age )
VALUES ( @TmpID,@Name,@Phone,@Address,@Age)

SET @Sum = @TmpID
SET NOCOUNT OFF
END

public void AddTeacherInfo()
{
using (DemoDBEntities entity = new DemoDBEntities())
{
var outputParameter = new ObjectParameter("sum", typeof(Guid));

var dd = entity.usp_AddTeacherInfo("dapeng", "021-99999999", "address222", 86, outputParameter);

}
}

EF 接收OUTPUT参数的方法 How to Retrieve Stored Procedure Output Parameters in Entity Framework的更多相关文章

  1. 【spring mvc】spring mvc POST方式接收单个字符串参数,不加注解,接收到的值为null,加上@RequestBody,接收到{"uid":"品牌分类大”},加上@RequestParam报错 ---- GET方式接收单个参数的方法

    spring mvc POST方式 接收单个参数,不加任何注解,参数名对应,接收到的值为null spring mvc POST方式 接收单个参数,加上@RequestBody,接收到参数格式:{&q ...

  2. [转]Easy Stored Procedure Output Oracle Select

    本文转自:http://www.oraclealchemist.com/oracle/easy-stored-procedure-output/ I answered a question on a ...

  3. EF报错:对一个或多个实体的验证失败(Entity Framework 强制转换失败数据异常处理方法)

    1.使用MVC和EF,在保存数据的时候报错:System.Data.Entity.Validation.DbEntityValidationException: 对一个或多个实体的验证失败.有关详细信 ...

  4. EF 6 调用存储过程时返回多结果集和OUTPUT参数问题

    原文地址:http://q.cnblogs.com/q/56836/ 各位大侠,提问一个关于EF6调用存储过程时返回多结果集和OUTPUT参数问题 目前已经可以调用存储过程并且可以返回多个结果集. 但 ...

  5. springmvc 传递和接收数组参数

    java url中如何传递数组,springMVC框架controller类如何接收数组参数? 下面介绍一下URL中传递数组参数方法: dd.do?titles[]=col1&titles[] ...

  6. How to call a stored procedure in EF Core 3.0 via FromSqlRaw(转载)

    问: I recently migrated from EF Core 2.2 to EF Core 3.0. Unfortunately, I haven't found a way to call ...

  7. EF Power Tools参数不正确的解决方法

    在Visual Studio 2010安装了EF Power Tools Beta 3之后,希望根据本地现有数据库模型来生成基于Entity Framework Code First的代码时,经常出现 ...

  8. struts2视频学习笔记 11-12(动态方法调用,接收请求参数)

    课时11 动态方法调用 如果Action中存在多个方法时,可以使用!+方法名调用指定方法.(不推荐使用) public String execute(){ setMsg("execute&q ...

  9. Struts2中Action接收参数的方法

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt112 Struts2中Action接收参数的方法主要有以下三种: 1.使用A ...

随机推荐

  1. sql为了实现转换的行列

    全名 学科 成绩 牛芬 语文 81 牛芬 数学 88 牛芬 英语 84 张三 语文 90 张三 数学 98 张三 英语 90 (表一) 现有一个表如(表一) 姓名 语文 数学 英语 牛芬 81 88 ...

  2. MaterialDialog的用法:

    MaterialDialog的用法:/** * * @author smiling * @date 2016/10 */ Github:https://github.com/drakeet/Mater ...

  3. iis7 发布mvc 遇到的HTTP错误 403.14-Forbidden Web 服务器被配置为不列出此目录的内容

    iis 7上发布mvc报错:403.14-Forbidden Web 服务器被配置为不列出此目录的内容 提示里面的解决方法是: 如果不希望启用目录浏览,请确保配置了默认文档并且该文件存在. 使用 II ...

  4. HTML中属性ID和属性NAME的区别(转)

    ID和Name都可以用来标识一个标记,Javascript分别有两个方法getElementById和getElementByName来定位Dom节点. 区别如下: 1.我们知道在网页做Post提交时 ...

  5. linux 第三天11讲

    1.linux下的shell 查看目前使用的是哪种shell env [该命令可以显示当前操作系统的环境变量] env | more chsh -s 输入新的shell 如:/bin/csh shel ...

  6. Mybatis的学习总结(一)——使用配置文件实现增删改查

    在使用Mybatis作为持久层来进行操作数据库,有很多的操作都是一样的,基本上都是先得到session,然后调用session提供的相关方法进行操作,接着提交session,最后关闭session.那 ...

  7. Oracle数据库之序列

    Oracle数据库之序列(sequence) 序列是一个计数器,它并不会与特定的表关联.我们可以通过创建Oracle序列和触发器实现表的主键自增.序列的用途一般用来填充主键和计数. 一.创建序列 语法 ...

  8. php中文匹配

    PHP判断字符串中是否含有中文 <? $str = "测试中文"; echo $str; echo "<hr>"; //if (preg_ma ...

  9. GitHub与VS2013完成项目管理

    https://github.com 程序员应该去注册一个账号的网站 1.创建一个仓库 登录你的github网站:找到新建一个仓库的入口 一些基本信息填写完毕后,点击创建,即可拥有一个仓库 2. 让V ...

  10. Jasper_filter data_pass field data from main to sub to filter some data

    main report: 1 add variable <variable name="Variable_rule" class="java.lang.String ...