Search code examples
asp.net-coreentity-framework-corerepository-patternunit-of-workef-fluent-api

i can't get all Categories data with ChildCategories


I'm trying to get all Categories data with SubCategories and ChildCategories but i cant reach to my ChildCategories on my razor page. I'm using Entity Framework Core 5.0 and code-first approach with UnitOfWork design pattern

Here is my repository with interface:

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();
    }
}

I'm doing using as a first time so I don't have a any information about it. Can you guys point me to which place I'm wrong?

It works when I call Categories with SubCategories like that:

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

Here is my 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; }
}

Here is my 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();
    }
}

Here is my 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();
    }
}

EDIT: I found the real problem. thanks for you guys I learn I have to use ThenInclude after it and EF Core is not recognizes that method.

@mj1313 has show me the real way you can scroll and find it


Solution

  • Try using ThenInclude() to be further include based on a related type that was just included.

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