I have 2 entities with a One-to-One relationship, the models are:
public class Asset
{
public int Id { get; set; }
public string Name { get; set; }
public TrackingDevice TrackingDevice { get; set; }
}
public class TrackingDevice
{
public int Id { get; set; }
public string Imei { get; set; }
public int? AssetId { get; set; }
public Asset Asset { get; set; }
}
I have entered data so when I make a simple query as follows:
var list = _appContext.TrackingDevices.Include(td => td.Asset).ToListAsync();
I get correctly the list of tracking devices that include their asset, however, the asset child again includes the tracking device, and this in turn the asset and so on, which creates an infinite structure that when applying the AutoMapper
failure.
How can I do the Include and only get the 2 levels I need? Tracking Device -> Asset
This is really annoying in EF Core.
I could not solve it completely but here is a wrok-around about that Not a complete solution
var list = _appContext.TrackingDevices.Include(td => td.Asset).ToListAsync();
foreach(var l in list)
l.Asset.TrackingDevice = null;