2

为了模拟延迟加载,我想要一个方法,通过急切加载递归地包含完整的对象图,以便在加载实体时,也加载其所有相关数据。

从 MSDN 文档:

  • 要包含单个引用:query.Include(e => e.Level1Reference)。
  • 要包含单个集合:query.Include(e => e.Level1Collection)。
  • 要包含一个参考,然后是一个参考向下一级:query.Include(e => e.Level1Reference.Level2Reference)。
  • 要包含一个引用,然后包含一个向下一级的集合:query.Include(e => e.Level1Reference.Level2Collection)。
  • 要包含一个集合,然后包含一个参考,请执行以下操作:query.Include(e => e.Level1Collection.Select(l1 => l1.Level2Reference))。
  • 要包含一个集合,然后包含一个向下一级的集合:query.Include(e => e.Level1Collection.Select(l1 => l1.Level2Collection))。
  • 要包含一个集合,然后包含一个参考,请执行以下操作:query.Include(e => e.Level1Collection.Select(l1 => l1.Level2Reference))。
  • 要包含一个集合,然后包含一个向下一级的集合:query.Include(e => e.Level1Collection.Select(l1 => l1.Level2Collection))。
  • 要包含一个集合、一个引用和一个向下两个级别的引用:query.Include(e => e.Level1Collection.Select(l1 => l1.Level2Reference.Level3Reference))。
  • 要包含一个集合、一个集合和一个参考向下两个级别:query.Include(e => e.Level1Collection.Select(l1 => l1.Level2Collection.Select(l2 => l2.Level3Reference)))。

问题:

如何递归地包含所有可导航属性并将其构建到通用存储库方法中,以便我可以在需要时获得实体的深层对象图,无论是否添加了新属性?

4

2 回答 2

3

好的,这是一个经过编辑的版本,应该能更好地满足您的要求:

private static void EnumerateAllIncludesList(DbContext context, IEnumerable entities, List<object> entitiesLoaded = null)
{
    if (entitiesLoaded == null)
        entitiesLoaded = new List<object>();

    foreach (var entity in entities)
        EnumerateAllIncludesEntity(context, entity, entitiesLoaded);

}

private static void EnumerateAllIncludesEntity(DbContext context, object entity, List<object> entitiesLoaded)
{
    if (entitiesLoaded.Contains(entity))
        return;

    entitiesLoaded.Add(entity);

    Type type = entity.GetType();
    var properties = type.GetProperties();

    foreach (var propertyInfo in properties)
    {
        var propertyType = propertyInfo.PropertyType;

        bool isCollection = propertyType.GetInterfaces().Any(x => x == typeof(IEnumerable)) &&
                            !propertyType.Equals(typeof(string));

        if (isCollection)
        {
            var entry = context.Entry(entity);

            if(entry.Member(propertyInfo.Name) as DbCollectionEntry == null)
                continue;

            entry.Collection(propertyInfo.Name).Load();

            var propertyValue = propertyInfo.GetValue(entity);

            if (propertyValue == null)
                continue;

            EnumerateAllIncludesList(context, (IEnumerable)propertyValue, entitiesLoaded);
        }
        else if ((!propertyType.IsValueType && !propertyType.Equals(typeof(string))))
        {
            var entry = context.Entry(entity);

            if (entry.Member(propertyInfo.Name) as DbReferenceEntry == null)
                continue;

            entry.Reference(propertyInfo.Name).Load();

            var propertyValue = propertyInfo.GetValue(entity);

            if (propertyValue == null)
                continue;

            EnumerateAllIncludesEntity(context, propertyValue, entitiesLoaded);
        }
        else
            continue;
    }
}

你会这样使用它:

using (var context = new MyContext())
{
    var result = context.People.Where(x => x.Id == 1).ToList();
    EnumerateAllIncludesList(context,result);
}
于 2013-03-22T14:40:10.523 回答
0

如果您使用模型优先或数据库优先,您可以编写一些 T4 模板来使用 edmx 模型生成您需要的内容。这并不容易,但可能。

于 2013-03-22T14:12:22.467 回答