asp.net-coreentity-framework-coreef-core-6.0

EF Core 6 objects dont load parent or child entities


Referring to this article:

From what I understand, EF Core 6 object relationships are automagically mapped if your objects are defined per the example below:

public class Device
{
    public int Id { get; set; }

    public string Name { get; set; }

    public int DeviceGroupId { get; set; }

    public DeviceGroup DeviceGroup { get; set; }
}

public class DeviceGroup
{
    public int Id { get; set; }

    public string Name { get; set; }

    public ICollection<Device> Devices { get; set;}
}

However, its not working for me.

In my DB tables:

I understand you can also specify these relationships explicitly by override the OnModelCreating of the DbContext, but I'd prefer to understand why my implementation above isnt working.


Solution

  • EF Core by default provides LazyLoading, you have to use Eager Loading if you want to include all the related entities. That is done using .Include() method. Which can also be chained using .ThenInclude().