Include in EF Core 2.0 create infinite nested child Entities

asp.net-core automapper entity-framework entity-framework-core

Question

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

1
1
12/11/2018 5:48:38 PM

Popular Answer

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;

0
10/11/2019 6:09:48 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow