I want to get all contact objects (including Person -> Employees)
Contact |<- Person | |-> Employer | |<- Organization | |-> Employees
The user and organization inherit from Contact.
When I use FirstOrDefault, my employees and my employer's facility were not loaded.
public Contact GetContactById(int id) { var contact = GetContacts().FirstOrDefault(c => c.Id == id); return contact; } private IQueryable<Contact> GetContacts() { var contacts = _contextProvider.Context.Contacts .Include("Addresses"); return contacts; }
What is my current workaround:
public Contact GetContactById(int id) { var contact = GetContacts().FirstOrDefault(c => c.Id == id); if (contact is Organization) { var organization = contact as Organization; _contextProvider.Context.Entry(organization).Collection(o => o.Employees).Load(); } else if (contact is Person) { var person = contact as Person; _contextProvider.Context.Entry(person).Reference(o => o.Employer).Load(); } return contact; }
Is there a better way to solve this problem?
source share