Lee*_*son 46 linq entity-framework
如果我使用连接,则Include()方法不再有效,例如:
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
Run Code Online (Sandbox Code Playgroud)
e.Properties 没有加载
没有连接,Include()可以工作
背风处
Ale*_*mes 54
更新:实际上我最近添加了另一个提示,涵盖了这一点,并提供了另一种可能更好的解决方案.我们的想法是延迟使用Include()直到查询结束,有关详细信息,请参阅此内容:提示22 - 如何使include真正包含
使用Include()时,实体框架存在已知限制.Include不支持某些操作.
看起来你可能已经碰到了这些限制,要解决这个问题你应该尝试这样的事情:
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};
Run Code Online (Sandbox Code Playgroud)
这将返回属性,如果实体和属性之间的关系是一对多(但不是多对多),您会发现每个生成的匿名类型具有相同的值:
anonType.Entity.Properties
anonType.Properties
Run Code Online (Sandbox Code Playgroud)
这是实体框架中称为关系修正的功能的副作用.
小智 20
试试这个:
var query = (ObjectQuery<Entities>)(from e in dc.Entities
join i in dc.Items on e.ID equals i.Member.ID
where (i.Collection.ID == collectionID)
select e)
return query.Include("Properties")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
48217 次 |
| 最近记录: |