Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InMemory DB doesn't return nested objects

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:

enter image description here

(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");
        }));
like image 332
Freddyerf Avatar asked Sep 23 '26 06:09

Freddyerf


1 Answers

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

like image 110
Patrick Mcvay Avatar answered Sep 24 '26 20:09

Patrick Mcvay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!