gsharp
gsharp

Reputation: 27927

Include / ThenInclude in EF Core

Let's say I have DB Tables like that:

Continent -> Countries -> Cities
                       -> Lakes

Now i want to include them

_db.Continents
 .Include(p => p.Countries)
   .ThenInclude(c => c.Cities)
 .Include(p => p.Countries)
   .ThenInclude(p => p.Lakes)

Is this the only way (by including countries twice) or is there another way?

Upvotes: 1

Views: 730

Answers (2)

Turner Bass
Turner Bass

Reputation: 821

There is actually support for this as of EF Core 2.1. The pattern looks something like this:

_db.Continents.Include(p => p.Countries).ThenInclude(c => c.Cities).ThenInclude((Country p) => p.Lakes)

Upvotes: 3

OrcusZ
OrcusZ

Reputation: 3660

As said before EF Include countries only once.

for better reading you can write it like that :

_db.Continents
 .Include(p => p.Countries.Select(s => s.Cities))
 .ThenInclude(p => p.Countries.Select(s => s.Lakes))

Upvotes: -2

Related Questions