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");
}));
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0
