Entityframework - Include nested children from parent

entity-framework entity-framework-6 linq-to-entities

Question

I'm trying to load two children from a nested parent.

dbContext.
.Where(f => f.Id == Tenant.Id)
.Include(f => f.Users
.Select(x=>x.Nicknames)
.Select(x => x.FavoriteMovies))
.SingleOrDefault();

So above I have a Tenant which has many users. Each user has two children that is one to many. Those are Nicknames and FavoriteMovies. Ex. User John has two nicknames JJ and Johnny and he has the following favorite movies Die Hard 1 and Die hard 2.

This works to get one of the children but how do I get the second one? I ultimately want to return tenant object.

  dbContext.
    .Where(f => f.Id == Tenant.Id)
    .Include(f => f.Users
    .Select(x=>x.Nicknames)
    .SingleOrDefault();
1
0
4/17/2020 10:38:12 PM

Popular Answer

Use the Include method to load multiple levels of related entities like this:


    dbContext.
    .Where(f => f.Id == Tenant.Id)
        .Include(t => t.Users.Select(un => un.Nicknames))
        .Include(t => t.Users.Select(uf => uf.FavoriteMovies))
    .SingleOrDefault();

0
4/17/2020 11:43:28 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