尝试从存储过程读取多个实体结果集时出现问题 [英] Issue when trying to read multiplte entity resultsets from a stored procedure

查看:137
本文介绍了尝试从存储过程读取多个实体结果集时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在MS SQL Server中使用EF5.0和代码优先方法
我已经阅读了一篇文章
http://msdn.microsoft.com/en-us/data/jj691402.aspx



和但是,假设我的商店程序包含这样的查询

  SELECT * from [dbo]。[MyEntities] as MyEntity 
其中ID = @ID

,C#中的代码是

  var entities = Context.ObjectContext.Translate< MyEntity> (读者,MyEntity,MergeOption.AppendOnly); 

我得到了这一点



System.Data.Entity.dll中出现类型为System.InvalidOperationException的异常,但未在用户代码中处理



附加信息:无法找到EntitySet名称MyDbContext.MyEntity


所以,显然它会添加一些上下文名称作为EntitySet名称的前缀,而不是MyEntity在结果集中寻找MyDbContext.MyEntity。



什么原因导致此行为,如果有任何工作(因为在上面引用的例子中,它看起来相当前卫和简单,并且不需要特定的操作,除了调用db.Database.Initialize(force:false);(我在我的代码中也执行)

解决方案

我自己找到一个答案
如果表别名不起作用,并且代码优先映射在en在调用Translate



时,我们不需要考虑类和表名称,所以我需要在这行代码中指定原始表名(MyEntities)而不是MyEntity, p>

  var entities = Context.ObjectContext.Translate< MyEntity>(reader,MyEntities,MergeOption.AppendOnly); 


We using EF5.0 and code-first approach with MS SQL Server I have read an article http://msdn.microsoft.com/en-us/data/jj691402.aspx

and decided to try the same approach over our database

however, suppose that my stores procedure contains a query like this

SELECT * from [dbo].[MyEntities] as MyEntity
    where ID = @ID

and code in C# is

var entities = Context.ObjectContext.Translate<MyEntity>(reader, "MyEntity", MergeOption.AppendOnly);

I am getting at this point

An exception of type 'System.InvalidOperationException' occurred in System.Data.Entity.dll but was not handled in user code

Additional information: The EntitySet name 'MyDbContext.MyEntity' could not be found.

So, obviously it adds some context name as a prefix to the EntitySet name and instead of MyEntity is looking for MyDbContext.MyEntity in the result set.

What causes this behaviour and if there is any workaroud (because in example I referenced above it looks quite streightforward and simple and no specific manipulations is needed, except call db.Database.Initialize(force: false); (which I do in my code as well)

解决方案

Well I find an answer myself If seems that table aliases not working and code-first mapping configurations between the entity classes and table name also not taken into account when you call Translate

So I need to specify original table name (MyEntities) instead of MyEntity in this line of code

var entities = Context.ObjectContext.Translate<MyEntity>(reader, "MyEntities", MergeOption.AppendOnly);

这篇关于尝试从存储过程读取多个实体结果集时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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