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:
Device.Id
and DeviceGroup.Id
are primary keys (integer, identity)Device.DeviceGroupId
is an integer with foreign key defined between Device.DeviceGroupId
and DeviceGroup.Id
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.
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()
.