EntityFramework – Include ThenInclude inconsistent in retrieving all data

I’m trying to create a new function in an existing project to retrieve data from the database, but i’m seeing inconsistent results compared to one of the existing functions and I’m not sure what I’ve done differently.

I’ve renamed the classes but tried to keep the same spirit of the relations.

Existing classes

public class Person
{
public int Id;
public string Name;
public int? ParentId;
public virtual Person Parent;
}

public class Family
{
public int Id;
public string Name;
public virtual ICollection<FamilyPerson> FamilyMembers;
}

public class FamilyPerson
{
public int Id;
public int FamilyId;
public virtual Family Family;
public int PersonId;
public virtual Person Person;
}

public class CoursePerson
{
public int Id;
public int CourseId;
public virtual Course Course;
public int PersonId;
public virtual Person Person;
}

The Database contains a table for each of the above classes.
The database contains a table Persons which has multiple Person’s.
When a Person gets added to the database, if they have a Parent, they get created with the link to their Parent, who can have a link to their Parent, going back three or four generations or potentially more.

A Family contains a collection of FamilyPerson which connects one family to multiple Person’s

I tried to create a get request to get the Family from the database, including the FamilyMembers collection and including each Person, along with their Parents, Parent of Parent and so on until reaching the Person who has no parent.

Family family = await _context.Familys
                .Include(family => family.FamilyMembers)
                .ThenInclude(familyMember => familyMember.Person)
                .ThenInclude(person => person.Parent)
                .Where(family => family.Name == familyName)
                .FirstOrDefaultAsync();

When I use this request, I am able to get the data for the Family, FamilyMembers, Person and Parent of Person, but not the parent of the parent when they do exist.
I could copy paste

.ThenInclude(person => person.Parent)

for as many times as I expect the generations to go back, but I’m looking for a more dynamic solution.

There is an existing service call for Courses that retrieves a list of CoursePerson relations that is similar.

List<CoursePerson> coursePersons = await _context.CoursePersons
                .Include(coursePerson => coursePerson.Person)
                .ThenInclude(person => person.Parent)
                .Where(coursePerson => coursePerson.CourseId == courseId).ToListAsync();

            return coursePersons;

and this call will include all of the parent generations until reaching the top of the tree.
Both requests have lazyLoading disabled, I tried using lazyLoading but it would retrieve too much data and slowed the service down.

I’m not sure why my new request only includes parents explicitly asked for, whereas the existing service retrieves all generations

New contributor

user26707598 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật