我正在构建ASP.NETCore2.0MVC应用程序。我有以下自我引用类:
public class ProductCategory
{
public int ID { get; set; }
public string Name { get; set; }
[DataType(DataType.Html)]
public string Description { get; set; }
public int DisplayOrder { get; set; }
public int MenuIconXPosition { get; set; }
public int MenuIconYPosition { get; set; }
public virtual ICollection<ProductSubCategory> ParentCategory { get; set; }
public virtual ICollection<ProductSubCategory> ChildCategory { get; set; }
public ICollection<ProductProductCategory> ProductProductCategories { get; set; }
}使用以下关系类:
public class ProductSubCategory
{
public int ChildCategoryId { get; set; }
public virtual ProductCategory ChildCategory { get; set; }
public int ParentCategoryId { get; set; }
public virtual ProductCategory ParentCategory { get; set; }
}在dbContext.cs文件中,我有以下内容:
modelBuilder.Entity<ProductSubCategory>()
.HasKey(p => new { p.ChildCategoryId, p.ParentCategoryId });
modelBuilder.Entity<ProductSubCategory>()
.HasOne(p => p.ParentCategory)
.WithMany(p => p.ChildCategory)
.HasForeignKey(p => p.ParentCategoryId);
modelBuilder.Entity<ProductSubCategory>()
.HasOne(p => p.ChildCategory)
.WithMany(p => p.ParentCategory)
.HasForeignKey(p => p.ChildCategoryId);数据库迁移和更新按预期构建。用数据填充它,它似乎像预期的那样工作。当使用以下LINQ查询迭代数据时:
var result = _context.ProductCategories
.AsNoTracking()
.Include(i => i.ParentCategory)
.Include(i => i.ChildCategory)
.OrderBy(i => i.DisplayOrder);预期的结果是Result -> ICollection<ProductCategory> -> ICollection<ProductSubCategory> -> ICollection<ProductCategory>但有时最后的推论是无效的。
知道我错过了什么吗,还是不理解?
发布于 2018-08-07 10:37:08
var result = _context.ProductCategories
.Include(i => i.ParentCategory)
.Include(i => i.ChildCategory)
.AsNoTracking()
.OrderBy(i => i.DisplayOrder)https://stackoverflow.com/questions/-100005920
复制相似问题