Asp.net core 我能';无法使用ChildCategories获取所有类别数据
我正在尝试获取所有Asp.net core 我能';无法使用ChildCategories获取所有类别数据,asp.net-core,entity-framework-core,repository-pattern,unit-of-work,ef-fluent-api,Asp.net Core,Entity Framework Core,Repository Pattern,Unit Of Work,Ef Fluent Api,我正在尝试获取所有类别数据以及子类别和儿童类别,但我无法在我的razor页面上找到我的儿童类别。我在UnitOfWork设计模式中使用实体框架核心5.0和代码优先的方法 这是我的存储库和界面: public interface IRepositoryCategory<T> : IRepository<Category> { IEnumerable<Category> GetCategoriesWithChildrens(); } public cla
类别
数据以及子类别
和儿童类别
,但我无法在我的razor页面上找到我的儿童类别
。我在UnitOfWork设计模式中使用实体框架核心5.0和代码优先的方法
这是我的存储库和界面:
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();
}
}
这是我的类别
课程:
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; }
}
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
课程:
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();
}
}
公共类ChildCategory:基本类
{
公共子类别(){}
public int ChildCategoryID{get;set;}
公共int子类别ID{get;set;}
公共字符串ChildCategoryName{get;set;}
公共字符串ChildCategoryPhoto{get;set;}
公共字符串ChildCategoryLink{get;set;}
public bool childcategority{get;set;}
public bool childcategoriyisactive{get;set;}
公共虚拟子类别子类别{get;set;}
}
公共类子类别配置:IEntityTypeConfiguration
{
公共void配置(EntityTypeBuilder)
{
HasKey(childcategory=>childcategory.ChildCategoryID);
builder.HasOne(subc=>subc.SubCategory)
.WithMany(subcc=>subcc.ChildCategories)
.HasForeignKey(子类别=>子类别.subcategory ID)
.OnDelete(DeleteBehavior.NoAction)
.IsRequired();
}
}
编辑:我发现了真正的问题。谢谢你们,我知道我必须使用,然后在它后面包含,EF Core不认可这种方法
@mj1313向我展示了滚动并找到它的真实方式尝试使用然后使用include()
根据刚才包含的相关类型进一步包含
return TradeTurkDBContext.Categories.Include(c => c.SubCategories).ThenInclude(sc => sc.ChildCategories).ToList();
尝试使用thenclude()
根据刚才包含的相关类型进一步包含
return TradeTurkDBContext.Categories.Include(c => c.SubCategories).ThenInclude(sc => sc.ChildCategories).ToList();
使用Microsoft.EntityFrameworkCore添加对的引用代码>
使用。然后包括(a=>a.ChildCategory)
使用Microsoft.EntityFrameworkCore添加对的引用代码>
使用.thenclude(a=>a.ChildCategory)
它说“iqeryable”不包含“thenclude”的定义,并且没有可访问的扩展方法“thenclude”可以找到“接受类型为“iqeryable”的第一个参数”``使用lambda而不是字符串。你能更新你的答案吗?我想接受你的答案,因为它是有效的,谢谢你的努力说,`` IQueryable'不包含'ThenClude'的定义,没有可访问的扩展方法',ThenClude'可以找到接受'IQueryable'类型的第一个参数``使用lambda而不是字符串。你能更新你的答案吗?我想接受你的答案,因为它是有效的,谢谢你的努力说,`` IQueryable'不包含'Thenclude'的定义,并且没有可访问的扩展方法',Thenclude'可以找到接受'IQueryable'类型的第一个参数``你有使用Microsoft.EntityFrameworkCore的参考资料吗;是的,我有那个参考资料。那个系统呢。林克?是的,我也有那个。我已经准备好重建和清理我的解决方案,它仍然是一样的,上面写着“IQueryable”不包含“thenClude”的定义,并且没有可访问的扩展方法“thenClude”可以找到“接受类型为“IQueryable”的第一个参数”``您是否有使用Microsoft.EntityFrameworkCore的参考;是的,我有那个参考资料。那个系统呢。林克?是的,我也有那个。我已经准备好重建和清理我的解决方案,它仍然是一样的