Linq-to-entities - Include()方法不加载 [英] Linq-to-entities - Include() method not loading

查看:115
本文介绍了Linq-to-entities - Include()方法不加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用连接,则Include()方法将不再工作,例如:

If I use a join, the Include() method is no longer working, eg:

from e in dc.Entities.Include("Properties")
join i in dc.Items on e.ID equals i.Member.ID
where (i.Collection.ID == collectionID) 
select e

e.Properties 未加载

没有加入,Include()工作

Without the join, the Include() works

Lee

推荐答案

更新:实际上,我最近添加了另外一个提示,涵盖了这一点,并提供了一个替代可能更好的解决方案。这个想法是延迟使用Include()直到查询结束,有关详细信息,请参阅:提示22 - 如何使包含真的包括

UPDATE: Actually I recently added another Tip that covers this, and provides an alternate probably better solution. The idea is to delay the use of Include() until the end of the query, see this for more information: Tip 22 - How to make include really include

当使用Include()时,实体框架中存在已知的限制。
某些操作只适用于Include。

There is known limitation in the Entity Framework when using Include(). Certain operations are just not supported with Include.

看起来您可能会遇到这些限制,为了解决这个问题,您应该尝试这样的事情:

Looks like you may have run into one on those limitations, to work around this you should try something like this:

var results = 
   from e in dc.Entities //Notice no include
   join i in dc.Items on e.ID equals i.Member.ID
   where (i.Collection.ID == collectionID) 
   select new {Entity = e, Properties = e.Properties};

这将带回属性,如果实体和属性之间的关系是一对多但不是很多到很多),你会发现每个结果的匿名类型都具有相同的值:

This will bring back the Properties, and if the relationship between entity and Properties is a one to many (but not a many to many) you will find that each resulting anonymous type has the same values in:

anonType.Entity.Properties
anonType.Properties

这是实体框架中功能的副作用,称为关系解决。

This is a side-effect of a feature in the Entity Framework called relationship fixup.

看到这个提示1 在我的 EF Tips系列了解更多信息。

See this Tip 1 in my EF Tips series for more information.

这篇关于Linq-to-entities - Include()方法不加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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