Entity Framework Core 2 querying multi level collections with theniclude

.net-core-2.0 entity-framework-core include

Question

I created two classes:

public class A {
  public int Id { get; set; }
  public ICollection<B> Bs { get; set; }
}

public class B {
  public ICollection<C> C1s { get; set; }
  public ICollection<C> C2s { get; set; }
}

then I tried to fetch them with ThenInclude method:

var result = context.As          //public DbSet<A> As { get; set; }
  .Include(a => a.Bs)
    .ThenInclude(b => b.C1s)
  .Include(a => a.Bs)
    .ThenInclude(b => b.C2s)
  .SingleOrDefaultAsync(a => a.Id.Equals(id)); //id is given

return await result;

But unfortunately both C1s and C2s collections are empty.

How to retrieve C entities which are related to B one?

1
0
9/12/2018 4:13:46 PM

Popular Answer

I replaced .ThenInclude() methods with

.Include("Points.MasterPoints")
.Include("Points.SlavePoints")

That solved my issue.

0
9/12/2018 4:37:40 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow