EF Core 3.1如何给IIncludableQueryable添加Tag且不改变其类型?
解决方案
核心是利用 IIncludableQueryable 本身继承自 IQueryable 的特性,给 ThenInclude 后的查询加标签后强制转换回 IIncludableQueryable 类型即可。EF Core 中 TagWith 仅修改查询表达式树的标签节点,不会改变查询的实际类型结构,强转操作完全安全。
你需要实现两个 ThenIncludeWithHint 重载,分别对应单实体导航属性和集合导航属性的后续关联场景:
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Query; using System.Linq.Expressions; public static class QueryHintsExtensions { // 保留原有其他方法不变,新增以下两个重载 /// <summary> /// 单实体导航属性的ThenInclude带提示重载 /// </summary> public static IIncludableQueryable<TEntity, TProperty> ThenIncludeWithHint<TEntity, TPrevious, TProperty>( this IIncludableQueryable<TEntity, TPrevious> source, Expression<Func<TPrevious, TProperty>> navigationPropertyPath, DbContext context, QueryHintsTypes hint, string optionalParameter = "") where TEntity : class { var tableName = GetTableName(typeof(TProperty), context); var includableQuery = source.ThenInclude(navigationPropertyPath); return (IIncludableQueryable<TEntity, TProperty>)includableQuery.AddTag(hint, tableName, optionalParameter); } /// <summary> /// 集合导航属性的ThenInclude带提示重载 /// </summary> public static IIncludableQueryable<TEntity, TProperty> ThenIncludeWithHint<TEntity, TPrevious, TProperty>( this IIncludableQueryable<TEntity, IEnumerable<TPrevious>> source, Expression<Func<TPrevious, TProperty>> navigationPropertyPath, DbContext context, QueryHintsTypes hint, string optionalParameter = "") where TEntity : class { var tableName = GetTableName(typeof(TProperty), context); var includableQuery = source.ThenInclude(navigationPropertyPath); return (IIncludableQueryable<TEntity, TProperty>)includableQuery.AddTag(hint, tableName, optionalParameter); } }
效果验证
实现后即可直接使用你预期的链式调用写法:
context.Games.AddHint(c, QueryHintsTypes.Forceseek) .IncludeWithHint(g => g.Events, c, QueryHintsTypes.Forceseek) .ThenIncludeWithHint(e => e.Team, c, QueryHintsTypes.Forceseek) .ThenIncludeWithHint(t => t.Players, c, QueryHintsTypes.Forceseek) .Where(...);
所有层级的关联表都会正确生成对应标签,原有的拦截器逻辑不需要做任何修改即可正常识别处理。
内容的提问来源于stack exchange,提问作者DorBS
相关产品推荐
相关产品推荐

