我似乎无法在孙子级别上加载InFrontPages
的产品或在此查询中更深入。产品是为第一级子类别加载的,但对于所有更深层次,它只返回null
:
var categories = await _context.ProductCategories
.Include(e => e.ProductInCategory)
.ThenInclude(e => e.Product)
.ThenInclude(f => f.InFrontPages)
.AsQueryable() // <-- Force full execution (loading) of the above
.Where(e => e.ParentId == id) // <-- then apply the parent id filter
// (id can be `null` (for root categories), or a category Id)
.OrderBy(o => o.SortOrder)
.ToListAsync();
模特:
public class ProductCategory
{
public int Id { get; set; }
public string Title { get; set; }
public int SortOrder { get; set; }
[ForeignKey(nameof(ParentCategory))]
public int? ParentId { get; set; }
public ProductCategory ParentCategory { get; set; } //nav.prop to parent
public ICollection<ProductCategory> Children { get; set; } = new List<ProductCategory>();
public ICollection<ProductInCategory> ProductInCategory { get; set; }
public ICollection<FrontPageProduct> FrontPageProduct { get; set; }
}
public class ProductInCategory
{
public int Id { get; set; }
public int ProductId { get; set; }
public int ProductCategoryId { get; set; }
public int SortOrder { get; set; }
public Product Product { get; set; }
public ProductCategory ProductCategory { get; set; }
}
public class Product
{
public int Id { get; set; }
public int? ProductTypeId { get; set; }
public string Title { get; set; }
public string Info { get; set; }
public string LongInfo { get; set; }
public decimal Price { get; set; }
public int? Weight { get; set; }
// A product can belong to multiple category front pages
public ICollection<FrontPageProduct> InFrontPages { get; set; }
// A product can belong to multiple categories
public ICollection<ProductInCategory> InCategories { get; set; }
}
public class FrontPageProduct
{
public int Id { get; set; }
public int ProductCategoryId { get; set; }
public int ProductId { get; set; }
public int SortOrder { get; set; }
public Product Product { get; set; }
public ProductCategory ProductCategory { get; set; }
}
我错过了什么?