这是一个有点老的问题,但由于它没有公认的答案,我想我会发布我的解决方案。
我正在使用 EF Core,并且想要做到这一点,从我的存储库类外部访问即时加载,这样我就可以指定每次调用存储库方法时要加载的导航属性。由于我有大量的表和数据,我不想要一组标准的急切加载实体,因为我的一些查询只需要父实体,而有些则需要整个树。
我当前的实现只支持IQueryable 方法(即FirstOrDefault、Where,基本上是标准的 lambda 函数),但我相信您可以使用它来传递到您的特定存储库方法。
我从 EF Core 的 EntityFrameworkQueryableExtensions.cs 的源代码开始,其中定义了 Include 和 ThenInclude 扩展方法。不幸的是,EF 使用内部类IncludableQueryable 来保存先前属性的树,以允许以后包含强类型。但是,对此的实现只不过是 IQueryable 为前一个实体添加了一个额外的泛型类型。
我创建了自己的版本,我称之为IncludableJoin,它将IIncludableQueryable 作为构造函数参数并将其存储在私有字段中以供以后访问:
public interface IIncludableJoin<out TEntity, out TProperty> : IQueryable<TEntity>
{
}
public class IncludableJoin<TEntity, TPreviousProperty> : IIncludableJoin<TEntity, TPreviousProperty>
{
private readonly IIncludableQueryable<TEntity, TPreviousProperty> _query;
public IncludableJoin(IIncludableQueryable<TEntity, TPreviousProperty> query)
{
_query = query;
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator<TEntity> GetEnumerator()
{
return _query.GetEnumerator();
}
public Expression Expression => _query.Expression;
public Type ElementType => _query.ElementType;
public IQueryProvider Provider => _query.Provider;
internal IIncludableQueryable<TEntity, TPreviousProperty> GetQuery()
{
return _query;
}
}
注意内部GetQuery 方法。这在以后很重要。
接下来,在我的通用IRepository 接口中,我定义了预加载的起点:
public interface IRepository<TEntity> where TEntity : class
{
IIncludableJoin<TEntity, TProperty> Join<TProperty>(Expression<Func<TEntity, TProperty>> navigationProperty);
...
}
TEntity 泛型类型是我的 EF 实体的接口。我的通用存储库中Join 方法的实现是这样的:
public abstract class SecureRepository<TInterface, TEntity> : IRepository<TInterface>
where TEntity : class, new()
where TInterface : class
{
protected DbSet<TEntity> DbSet;
protected SecureRepository(DataContext dataContext)
{
DbSet = dataContext.Set<TEntity>();
}
public virtual IIncludableJoin<TInterface, TProperty> Join<TProperty>(Expression<Func<TInterface, TProperty>> navigationProperty)
{
return ((IQueryable<TInterface>)DbSet).Join(navigationProperty);
}
...
}
现在是实际允许多个Include 和ThenInclude 的部分。我有几个扩展方法可以接受和返回,IIncludableJoin 允许方法链接。在其中我调用 DbSet 上的 EF Include 和 ThenInclude 方法:
public static class RepositoryExtensions
{
public static IIncludableJoin<TEntity, TProperty> Join<TEntity, TProperty>(
this IQueryable<TEntity> query,
Expression<Func<TEntity, TProperty>> propToExpand)
where TEntity : class
{
return new IncludableJoin<TEntity, TProperty>(query.Include(propToExpand));
}
public static IIncludableJoin<TEntity, TProperty> ThenJoin<TEntity, TPreviousProperty, TProperty>(
this IIncludableJoin<TEntity, TPreviousProperty> query,
Expression<Func<TPreviousProperty, TProperty>> propToExpand)
where TEntity : class
{
IIncludableQueryable<TEntity, TPreviousProperty> queryable = ((IncludableJoin<TEntity, TPreviousProperty>)query).GetQuery();
return new IncludableJoin<TEntity, TProperty>(queryable.ThenInclude(propToExpand));
}
public static IIncludableJoin<TEntity, TProperty> ThenJoin<TEntity, TPreviousProperty, TProperty>(
this IIncludableJoin<TEntity, IEnumerable<TPreviousProperty>> query,
Expression<Func<TPreviousProperty, TProperty>> propToExpand)
where TEntity : class
{
var queryable = ((IncludableJoin<TEntity, IEnumerable<TPreviousProperty>>)query).GetQuery();
var include = queryable.ThenInclude(propToExpand);
return new IncludableJoin<TEntity, TProperty>(include);
}
}
在这些方法中,我使用上述GetQuery 方法获取内部IIncludableQueryable 属性,调用相关的Include 或ThenInclude 方法,然后返回一个新的IncludableJoin 对象以支持方法链接。
就是这样。 this的用法是这样的:
IAccount account = _accountRepository.Join(x=>x.Subscription).Join(x=>x.Addresses).ThenJoin(x=>x.Address).FirstOrDefault(x => x.UserId == userId);
上面将加载基础Account实体,它是一对一的子Subscription,它是一对多的子列表Addresses,它是子Address。沿途的每个 lambda 函数都是强类型的,并受智能感知支持以显示每个实体上可用的属性。