EF Core how select entity with many-to-many relationship

c# entity-framework entity-framework-core linq

Question

I ave table structure like this

  1. users
  2. user_profiles
  3. profiles

description:

user has many user profiles, user_profile table join user and profile tables together.(there is a many to many relationship between user and the profile table)

user>one-to-many>user_profiles>one-to-one>profiles

user>many user_profiles> one profile

Problem:

How can i select user with profile by using linq.

sample:

var user=cbContext.user.include("user_profiles").include("profiles").Where(predicate).FirstOrDefault();
1
6
10/1/2018 7:31:36 AM

Popular Answer

Found the answer

dbContext.Users
  .Include(user => user.UserProfiles)
  .ThenInclude(userProfiles => userProfiles.Profile) 
  .Where(predicate)
  .FirstOrDefault();
7
10/1/2018 6:55:00 AM


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