我遇到了一个由 POCO T4 模板创建的实体框架 POCO 的奇怪问题——对于某些对象,它们的集合属性创建EntityCollection为FixUpCollection.
我发现这三个类对产品层次结构进行建模;ProductGroup,Platform和Product. 每个ProductGroup都有Platforms 的集合,每个Platform都有 s 的集合Product。所有的关系都是双向的。每个类的集合 getter 和 setter 完全相同,因为它们是由 T4 模板生成的,所以它们看起来(例如)如下所示:
public virtual ICollection<Platform> Platforms
{
get
{
if (_platforms == null)
{
var newCollection = new FixupCollection<Platform>();
newCollection.CollectionChanged += FixupPlatforms;
_platforms = newCollection;
}
return _platforms;
}
set { ... }
}
有趣的是,所有onProduct和的集合Platform都被创建为EntityCollections,而所有on 的集合ProductGroup都被创建为FixUpCollectionsie 当代码第一次进入 (eg) 的 getter 时Platform.Products,该_products字段已经填充了 an EntityCollection,但是当它第一次进入上面显示的 getter_platforms为 null 并且 aFixupCollection被创建并随后填充。延迟加载在这两种情况下都有效,它只是以两种不同的方式工作。
该Entities对象启用了延迟加载和代理创建。Product,Platform和CoreProduct对象都是Entity.DynamicProxies命名空间中的动态 EF 代理。我试过急切地加载Platformand ProductGroup,这没有任何区别。我看不出模型查看器中类的设置方式有什么不同。
这让我很头疼,因为其中一个集合ProductGroup包含数千个对象,我想查询那个集合。据我所知(如果我错了,请纠正我)如果FixUpCollection不将所有对象加载到内存中,我就无法查询 a ,EntityCollection因为我可以使用CreateSourceQuery(). 有没有人见过这种行为?我在某处缺少某些设置吗?任何指示或帮助将不胜感激。