首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在.NET核心2.0中自我引用许多类而不返回集合?

在.NET核心2.0中自我引用许多类而不返回集合?
EN

Stack Overflow用户
提问于 2018-08-07 01:22:49
回答 1查看 0关注 0票数 0

我正在构建ASP.NETCore2.0MVC应用程序。我有以下自我引用类:

代码语言:txt
复制
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; }
        }

使用以下关系类:

代码语言:txt
复制
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文件中,我有以下内容:

代码语言:txt
复制
    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查询迭代数据时:

代码语言:txt
复制
    var result = _context.ProductCategories
        .AsNoTracking()
        .Include(i => i.ParentCategory)
        .Include(i => i.ChildCategory)
        .OrderBy(i => i.DisplayOrder);

预期的结果是Result -> ICollection<ProductCategory> -> ICollection<ProductSubCategory> -> ICollection<ProductCategory>但有时最后的推论是无效的。

知道我错过了什么吗,还是不理解?

EN

回答 1

Stack Overflow用户

发布于 2018-08-07 10:37:08

代码语言:txt
复制
 var result = _context.ProductCategories            
        .Include(i => i.ParentCategory)
        .Include(i => i.ChildCategory)            
        .AsNoTracking()
        .OrderBy(i => i.DisplayOrder)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100005920

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档