Linq-to-entities - Include() 方法未加载 [英] Linq-to-entities - Include() method not loading
问题描述
如果我使用连接,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
李
推荐答案
UPDATE:实际上,我最近添加了另一个涵盖此问题的提示,并提供了一个可能更好的替代解决方案.这个想法是将 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.
See this Tip 1 in my EF Tips series for more information.
这篇关于Linq-to-entities - Include() 方法未加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!