简体   繁体   English

如何在 EF4 中调用存储过程

[英]How to call stored procedure in EF4

I'm trying to call a stored procedure in Entity Framework 4 application and so far getting strange results.我正在尝试在 Entity Framework 4 应用程序中调用存储过程,但到目前为止得到了奇怪的结果。 Stored procedure takes IN and OUT parameters, and returns resultset.存储过程接受INOUT参数,并返回结果集。 I mapped stored procedure and created a complex type that represents a row in returned resultset.我映射了存储过程并创建了一个复杂类型,它表示返回的结果集中的一行。 I call it我称之为

using (MyObjectContext ctx = new MyObjectContext())
{
     ObjectParameter out1 = new ObjectParameter("out1", typeof(String));
     ObjectParameter out2 = new ObjectParameter("out2", typeof(String));     
     var res = ctx.my_proc(1,2, out1,out2);
}

The problem is that unless I call res.ToList() (or enumerate through res , or call any methods that accesses underlying collection), values of out1 and out2 are null .问题是,除非我调用res.ToList() (或通过res枚举,或调用任何访问底层集合的方法),否则out1out2的值是null
How do I fix it?我如何解决它?
Thanks谢谢

You cannot fix it.你无法修复它。 It is a gotcha.这是一个问题。 Result set must be materialized or thrown away before output parameters are accessible because these output parameters are transferred in the last result set so internal data reader must first read the returning result set with main data and then access the next result set to read output parameters.结果集必须在 output 参数可访问之前实现或丢弃,因为这些 output 参数在最后一个结果集中传输,因此内部数据读取器必须首先读取返回的结果集和主数据,然后访问下一个结果集以读取 Z78E6221F6393D14CE6DZ 参数。 Btw.顺便提一句。 it is the same if you use ADO.NET directly - that is not a feature of Entity framework itself but the way how SQL server returns output parameters and the way how DataReader consumes result sets in sequential order.如果您直接使用 ADO.NET 也是一样 - 这不是实体框架本身的功能,而是 SQL 服务器如何返回 output 参数的方式以及 DataReader 如何按顺序使用结果集的方式。

when u call procedure or query that returns a table of rows EF does real execution when user calls ToList or enumerate the return value当你调用返回行表的过程或查询时,当用户调用 ToList 或枚举返回值时,EF 会真正执行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM