For some reason I can't get all the related objects from my database. Maybe I made a mistake somewhere, but I can't see it. Can anyone help me out please.
I'm trying to get all the related objects from many-to-many relastionships using:
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();
But, past the last ThenInclude I can't go further. It treats a property after lambda as Offerings Collection. Ideally it should go further and look like this:
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();
This is my OnModelCreating method in the Context class:
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);
}
This is my Family and Offering Entities with navigation properties class. All Entities have many-to-many relationship.
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; }
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.