I have a scenario where a Manager (Employee
table) has several Desktops (Desktop
table) and several Reportees (Employee
table using self reference). I am using EF Core 1.x.
I have Desktop Id and Reportee Id as inputs and I want to retrieve Manager obkect filled with one Desktop object and one Reportee (Employee) object pertaining to supplied ids as expected output. So, I wrote following query:
var desktop = _desktopRepository.Query()
.Where(s => s.Id == desktopId)
.Include(s => s.Employee)
.All()
.FirstOrDefault();
// EF is magically adding reportee object to the desktop.Employee.Reportee list.
// So, no need to add it separately. If done, there'll be two objects of same Reportee.
var reportee = _employeeRepository.Get(reporteeId).Result;
What I observed here is written in comments. I am looking for guidance on why is EF automatically adding this object to the list.
It is important to note that here, I am firing two completely different queries. When I debug through the code, I find that it is firing them separately and fetching the result. The EF is not only adding the result into above entity, but also returning it as independent result.
NOTE: Sorry, but I am not looking if this is right or wrong approach. But I want to know how/why EF is adding this object automatically to the collection.
Any help is much appreciated. Many thanks in Advance.