我无法使用 ChildCategories 获取所有类别数据

i can't get all Categories data with ChildCategories

我正在尝试使用 SubCategoriesChildCategories 获取所有 Categories 数据,但我无法访问我的剃须刀页面上的 ChildCategories。我正在使用 Entity Framework Core 5.0 和带有 UnitOfWork 设计模式的代码优先方法

这是我的带有界面的存储库:

public interface IRepositoryCategory<T> : IRepository<Category>
{
    IEnumerable<Category> GetCategoriesWithChildrens();
}

public class RepositoryCategory<T> : Repository<Category>, IRepositoryCategory<T>
{
    public RepositoryCategory(TradeTurkDBContext context) : base(context) { }

    public IEnumerable<Category> GetCategoriesWithChildrens()
    {
        return TradeTurkDBContext.Categories.Include("SubCategories").Include("ChildCategories").ToList();
    }
}

我是第一次使用,所以我没有任何关于它的信息。 你们能指出我哪里错了吗?

当我用 SubCategories 调用 Categories 时它起作用了:

return TradeTurkDBContext.Categories.Include("SubCategories").ToList();

这是我的 Category class:

public class Category : Base
{
    public Category(){}

    [Key]
    public int CategoryID { get; set; }
    public string CategoryName { get; set; }
    public string CategoryPhoto { get; set; }
    public string CategoryLink { get; set; }
    public bool CategoryIsFeatured { get; set; }
    public bool CategoryIsActive { get; set; }
    public virtual ICollection<SubCategory> SubCategories { get; set; }
}

这是我的 SubCategory class:

public class SubCategory : Base
{
    public SubCategory(){}
    [Key]
    public int SubCategoryID { get; set; }
    public int CategoryID { get; set; }
    public string SubCategoryName { get; set; }
    public string SubCategoryPhoto { get; set; }
    public string SubCategoryLink { get; set; }
    public bool SubCategoryIsFeatured { get; set; }
    public bool SubCategoryIsActive { get; set; }
    public virtual Category Category { get; set; }
    public virtual ICollection<ChildCategory> ChildCategories { get; set; }
}

public class SubCategoryConfiguration : IEntityTypeConfiguration<SubCategory>
{
    public void Configure(EntityTypeBuilder<SubCategory> builder)
    {
        builder.HasKey(subcategory => subcategory.SubCategoryID);
        builder.HasOne(cat => cat.Category)
            .WithMany(cats => cats.SubCategories)
            .HasForeignKey(category => category.CategoryID)
            .OnDelete(DeleteBehavior.NoAction)
            .IsRequired();
    }
}

这是我的 ChildCategory class :

public class ChildCategory : Base
{
    public ChildCategory(){}
    public int ChildCategoryID { get; set; }
    public int SubCategoryID { get; set; }
    public string ChildCategoryName { get; set; }
    public string ChildCategoryPhoto { get; set; }
    public string ChildCategoryLink { get; set; }
    public bool ChildCategoryIsFeatured { get; set; }
    public bool ChildCategoryIsActive { get; set; }
    public virtual SubCategory SubCategory { get; set; }}
}

public class ChildCategoryConfiguration : IEntityTypeConfiguration<ChildCategory>
{
    public void Configure(EntityTypeBuilder<ChildCategory> builder)
    {
        builder.HasKey(childcategory => childcategory.ChildCategoryID);

        builder.HasOne(subc => subc.SubCategory)
            .WithMany(subcc => subcc.ChildCategories)
            .HasForeignKey(subcategory => subcategory.SubCategoryID)
            .OnDelete(DeleteBehavior.NoAction)
            .IsRequired();
    }
}

编辑:我发现了真正的问题。谢谢你们,我知道我必须在它之后使用 ThenInclude,而 EF Core 无法识别该方法。

@mj1313 向我展示了滚动并找到它的真实方法

尝试使用 ThenInclude() 根据刚刚包含的相关类型进一步包含。

return TradeTurkDBContext.Categories.Include(c => c.SubCategories).ThenInclude(sc => sc.ChildCategories).ToList();

添加对 using Microsoft.EntityFrameworkCore;

的引用

使用.ThenInclude(a => a.ChildCategory)