2

I have this relationship :

public class Company
{
  public virtual ICollection<Employee> Employee { get; set; }
}

Inside of Employee I have a Profile property:

public class Employee
{
    public virtual Profile Profile { get; set; }
}

I'm able to get all the employees but not the Profile property.

And the request :

var result = await Context.Company.Include(a => a.Employee).Where(a => a.Token == Token).SingleOrDefaultAsync();

The result is retrieved but not the Profile.

jcruz
  • 2,440
  • 3
  • 15
  • 31
Nathiel Paulino
  • 444
  • 3
  • 12
  • Try using the `ThenInclude` method: https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.entityframeworkqueryableextensions.theninclude?view=efcore-3.0 – jcruz Nov 12 '19 at 14:38
  • Possible duplicate of [Using EF Core ThenInclude() on Junction tables](https://stackoverflow.com/questions/40319251/using-ef-core-theninclude-on-junction-tables) – Jan Nov 12 '19 at 14:40

2 Answers2

3

as @jcruz pointed out ThenInclude in EF Core is what you want:

var result = await Context.Company.Include(a => a.Employee).ThenInclude(e => e.Profile).Where(a => a.Token == Token).SingleOrDefaultAsync();
Jan
  • 2,926
  • 2
  • 23
  • 35
  • I had found about the `ThenInclude` but it wasn't working, because the relationship from Profile to Employee one to one was made but in reverse, not is working. Thanks! – Nathiel Paulino Nov 12 '19 at 14:48
0

Change the Include() function to the string overload and include the Profile property.

var result = await Context.Company.Include("Employee.Profile").Where(a => a.Token == Token).SingleOrDefaultAsync();
Tanveer Badar
  • 4,541
  • 2
  • 27
  • 31