栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

实体框架核心2.0.1渴望在所有嵌套相关实体上加载

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

实体框架核心2.0.1渴望在所有嵌套相关实体上加载

该功能目前正式不存在(EF Core 2.0.2以及即将推出的2.1)。已在“
急切加载”中请求所有导航属性#4851(已关闭),目前已通过基于规则的急切加载(包括)#2953和允许在模型中声明聚合(例如,定义包含的属性或通过其他方式)进行跟踪#1985(均在积压中,即没有具体的时间表)。

我可以提供以下两种自定义扩展方法:

using System;using System.Collections.Generic;using System.Linq;using Microsoft.EntityframeworkCore.metadata;namespace Microsoft.EntityframeworkCore{    public static partial class CustomExtensions    {        public static IQueryable<T> Include<T>(this IQueryable<T> source, IEnumerable<string> navigationPropertyPaths) where T : class        { return navigationPropertyPaths.Aggregate(source, (query, path) => query.Include(path));        }        public static IEnumerable<string> GetIncludePaths(this DbContext context, Type clrEntityType)        { var entityType = context.Model.FindEntityType(clrEntityType); var includedNavigations = new HashSet<INavigation>(); var stack = new Stack<IEnumerator<INavigation>>(); while (true) {     var entityNavigations = new List<INavigation>();     foreach (var navigation in entityType.GetNavigations())     {         if (includedNavigations.Add(navigation))  entityNavigations.Add(navigation);     }     if (entityNavigations.Count == 0)     {         if (stack.Count > 0)  yield return string.Join(".", stack.Reverse().Select(e => e.Current.Name));     }     else     {         foreach (var navigation in entityNavigations)         {  var inverseNavigation = navigation.FindInverse();  if (inverseNavigation != null)      includedNavigations.Add(inverseNavigation);         }         stack.Push(entityNavigations.GetEnumerator());     }     while (stack.Count > 0 && !stack.Peek().MoveNext())         stack.Pop();     if (stack.Count == 0) break;     entityType = stack.Peek().Current.GetTargetType(); }        }    }}

第一种只是应用多个字符串库的便捷方法

Include

第二个步骤

Include
使用EF
Core提供的元数据来完成收集类型的所有路径的实际工作。它基本上是从所传递的实体类型开始的有向循环图处理,不包括所包含路径的逆向导航,仅发送到“叶”节点的路径。

您的示例中的用法可能是这样的:

public virtual async Task<IEnumerable<T>> GetAllAsync(expression<Func<T, bool>> predicate = null){    var query = Context.Set<T>()        .Include(Context.GetIncludePaths(typeof(T));    if (predicate != null)        query = query.Where(predicate);    return await query.ToListAsync();}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/506787.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号