我一直试图将多个包含项拆分为单独的方法。 这样,我希望能够从对象层次结构的任何位置以单一方法获取实体的所有子代。
说我要让作者拥有书籍和页面:
var result = Context.Authors.Include(x => x.Books).ThenInclude(x => x.Pages);
现在,显然,下面的方法行不通,但是它说明了我追求的那种解决方案:
internal static class IncluderExtension <T> where T : class
{
internal static IIncludableQueryable<T, List<Books>> IncludeBooks(this IIncludableQueryable<T, List<Author>> query)
{
var result = query.ThenInclude(x => x.Books).IncludePages();
return result;
}
internal static IIncludableQueryable<T, List<Page>> IncludePages(this IIncludableQueryable<T, List<Books>> query)
{
var result = query.ThenInclude(x => x.Page);
return result;
}
}
关于如何实现目标的任何想法?