Frameworks 实体框架6和显式加载对象图

Frameworks 实体框架6和显式加载对象图,frameworks,linq-to-entities,entity,loading,Frameworks,Linq To Entities,Entity,Loading,我有这个模型结构: public class Blog { public ICollection<Post> Posts { get; set; } } public class Post { public ICollection<Comment> Comments { get; set; } } public class Comment { } 公共类博客 { 公共ICollection Posts{get;set;} } 公营职位 { 公共IC

我有这个模型结构:

public class Blog
{
    public ICollection<Post> Posts { get; set; }
}

public class Post
{
    public ICollection<Comment> Comments { get; set; }
}

public class Comment
{

}
公共类博客
{
公共ICollection Posts{get;set;}
}
公营职位
{
公共ICollection注释{get;set;}
}
公开课评论
{
}
我想显式加载“帖子”和“评论”集合

不能使用延迟加载或快速加载(使用Include操作符),因为我加载的集合必须在加载到每个对象之前进行过滤(因为它们可能包含大量项)

关于实现这一目标的最佳方式有何建议

谢谢


Riana

我找到了一个灵感来源和基础的解决方案

我主要关心的是在加载博客聚合时,能够过滤掉“帖子”和“评论”集合(根据已知条件)

延迟加载不是一个好的解决方案,因为它会向数据库发送太多的查询,以便加载所有需要的帖子及其相应的评论(帖子和评论集合可能非常庞大)

我认为使用“Include”操作符的急切加载不是一个好的解决方案,因为“Include”操作符不支持过滤器。这意味着,如果我像这样加载我的博客:myContext.blogs.Include(“Posts”),则不会过滤Posts集合,并且将加载数据库中的所有帖子,我不希望这样

在EF6中,现在可以在查询转换为SQL并发送到数据库之前拦截和修改查询。此功能是关键的解决方案

我的解决方案是截取Post和Comment对象上的所有SELECT查询,并修改它们以应用我的过滤器。通过这个实现,我可以进行快速加载,并且仍然可以自动应用Post和Comment对象上的所有SELECT查询

Rowan Miller通过实现软删除来演示此功能

希望这有帮助,

里亚纳