I have an entity that looks like this:
public class Worker
{
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Department { get; set }
public List<Worker> Subordinates { get; set; }
}
And I am using an InMemory Database. The problem is that when I want to get the workers it returns null on the Subordinates property. However, when I debug it to see if the list of subordinates is loading it behaves as expected:

(It says count=2 but if I just run it without debugging it returns null for that property)
EDIT: this is the query:
public Worker GetById(string id)
{
return _workerContext.Workers.SingleOrDefault(e => e.Id == id);
}
EDIT: in-memory db:
services.AddDbContext<WorkerContext>((options =>
{
options.UseInMemoryDatabase("WorkerDB");
}));
You can try two different ways. The first way is to make the List<Subordinates> a virtual property (this will enable lazy loading).
public virtual List<Worker> Subordinates { get; set; }
The second way would be to explicitly include the List<Subordinates>.
return _workerContext.Workers.Include(w => w.Subordinates).SingleOrDefault(e => e.Id == id);
Now I am not sure that this will fix your issue, but it is something you can try. I am not very familiar with InMemory Databases
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With