实体框架无法获得所有相关对象

时间:2017-03-31 11:36:44

标签: c# entity-framework many-to-many entity-framework-core

出于某种原因,我无法从我的数据库中获取所有相关对象。也许我在某个地方犯了一个错误,但我无法看到它。任何人都可以帮帮我。

我试图通过以下方式从多对多关系中获取所有相关对象:

            var result = await ec.Organizations.Include(o => o.Countries)
            .ThenInclude(oc => oc.Country)
            .ThenInclude(c => c.Businesses)
            .ThenInclude(cb => cb.Business)
            .ThenInclude(b => b.Families)
            .ThenInclude(bf => bf.Family)
            .ThenInclude(f => f.Offerings)
            .ToListAsync();

但是,经过最后一次ThenInclude,我无法继续前进。它将lambda之后的属性视为Offerings Collection。 理想情况下它应该更进一步,看起来像这样:

            var result = await ec.Organizations.Include(o => o.Countries)
            .ThenInclude(oc => oc.Country)
            .ThenInclude(c => c.Businesses)
            .ThenInclude(cb => cb.Business)
            .ThenInclude(b => b.Families)
            .ThenInclude(bf => bf.Family)
            .ThenInclude(f => f.Offerings)
            .ThenInclude(fo => fo.Offering)
            .ThenInclude(o => o.Departments)
            .ThenInclude(od => od.Department)
            .ToListAsync();

这是我在Context类中的OnModelCreating方法:

protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<OrganizationCountry>()
           .HasKey(t => new { t.CountryId, t.OrganizationId });

        modelBuilder.Entity<OrganizationCountry>()
            .HasOne(oc => oc.Organization)
            .WithMany(o => o.Countries)
            .HasForeignKey(oc => oc.OrganizationId);

        modelBuilder.Entity<OrganizationCountry>()
            .HasOne(oc => oc.Country)
            .WithMany(c => c.Organizations)
            .HasForeignKey(oc => oc.CountryId);

        modelBuilder.Entity<CountryBusiness>()
            .HasKey(t => new { t.BusinessId, t.CountryId });

        modelBuilder.Entity<CountryBusiness>()
            .HasOne(cb => cb.Country)
            .WithMany(c => c.Businesses)
            .HasForeignKey(cb => cb.CountryId);

        modelBuilder.Entity<CountryBusiness>()
            .HasOne(cb => cb.Business)
            .WithMany(b => b.Countries)
            .HasForeignKey(cb => cb.BusinessId);

        modelBuilder.Entity<BusinessFamily>()
            .HasKey(t => new { t.FamilyId, t.BusinessId });

        modelBuilder.Entity<BusinessFamily>()
            .HasOne(bf => bf.Business)
            .WithMany(b => b.Families)
            .HasForeignKey(bf => bf.BusinessId);

        modelBuilder.Entity<BusinessFamily>()
            .HasOne(bf => bf.Family)
            .WithMany(f => f.Businesses)
            .HasForeignKey(bf => bf.FamilyId);

        modelBuilder.Entity<FamilyOffering>()
            .HasKey(t => new { t.OfferingId, t.FamilyId });

        modelBuilder.Entity<FamilyOffering>()
            .HasOne(fo => fo.Family)
            .WithMany(f => f.Offerings)
            .HasForeignKey(fo => fo.FamilyId);

        modelBuilder.Entity<FamilyOffering>()
            .HasOne(fo => fo.Offering)
            .WithMany(o => o.Families)
            .HasForeignKey(fo => fo.OfferingId);

        modelBuilder.Entity<OfferingDepartment>()
            .HasKey(t => new { t.DepartmentId, t.OfferingId });

        modelBuilder.Entity<OfferingDepartment>()
            .HasOne(od => od.Offering)
            .WithMany(o => o.Departments)
            .HasForeignKey(od => od.OfferingId);

        modelBuilder.Entity<OfferingDepartment>()
            .HasOne(od => od.Department)
            .WithMany(d => d.Offerings)
            .HasForeignKey(od => od.DepartmentId);
    }

这是我的家庭和提供实体的导航属性类。所有实体都有多对多的关系。

 public class Family
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public virtual ICollection<BusinessFamily> Businesses { get; set; }
        public virtual ICollection<FamilyOffering> Offerings { get; set; }

        public Family()
        {
            Offerings = new List<FamilyOffering>();
            Businesses = new List<BusinessFamily>();
        }
    }

public class Offering
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public virtual ICollection<FamilyOffering> Families { get; set; }
        public virtual ICollection<OfferingDepartment> Departments { get; set; }

        public Offering()
        {
            Departments = new List<OfferingDepartment>();
            Families = new List<FamilyOffering>();
        }
    }

public class FamilyOffering
    {
        public int FamilyId { get; set; }
        public Family Family { get; set; }

        public int OfferingId { get; set; }
        public Offering Offering { get; set; }
    }

1 个答案:

答案 0 :(得分:0)

它似乎是一些Visual Studio IntelliSense错误。 IntelliSense不显示FamilyOffering类型的属性,但它显示了List的属性。

IntelliSense don't show property

但是FamilyOffering类型的属性就在那里。没有显示错误

But it's there