2

I have the following classes (One-One relationship Asset-TrackingDevice):

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; }
    }

The viewModels are very similar:

public class AssetViewModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int? TrackingDeviceId { get; set; }
        public TrackingDeviceViewModel TrackingDevice { get; set; }

    }
public class TrackingDeviceViewModel
    {
        public int Id { get; set; }       
        public string Imei { get; set; }
        public AssetViewModel Asset { get; set; }
        public string AssetId { get; set; }
    }

Mappings:

CreateMap<Asset, AssetViewModel>()
                .ForMember(d => d.TrackingDevice, map => map.Ignore());
CreateMap<AssetViewModel, Asset>()
                .ForMember(d => d.TrackingDevice, map => map.Ignore());
CreateMap<AssetViewModel, Asset>()
                .ReverseMap();
CreateMap<TrackingDevice, TrackingDeviceViewModel>()
                .ForMember(d => d.Asset, map => map.Ignore());
CreateMap<TrackingDeviceViewModel, TrackingDevice>()
                .ForMember(d => d.Asset, map => map.Ignore());
CreateMap<TrackingDevice, TrackingDeviceViewModel>()
                .ReverseMap();

When I perform a database query to obtain the TrackingDevices, I get an error because in the mapping the Asset within Tracking Device also includes a Tracking Device and so on.

The query that I execute to obtain the tracking devices is:

var trackingDevices = _appContext.TrackingDevices
         .Include(td => td.Asset)
         .ToListAsync();
var trackingMapper = Mapper.Map<IEnumerable<TrackingDeviceViewModel>>(trackingDevices);

I read that by including the Map.Ignore would fix the problem but it did not work either, does anyone know what my error is?

Juan Calderon
  • 641
  • 1
  • 6
  • 9
  • Upgrade. This works on the latest without any ignore. – Lucian Bargaoanu Dec 05 '18 at 15:52
  • @LucianBargaoanu Hi, upgrade what¿? – Juan Calderon Dec 05 '18 at 15:57
  • Upgrade AutoMapper. – Lucian Bargaoanu Dec 05 '18 at 16:00
  • I upgrade AutoMapper to 8.00, I erase all map => map.Ignore()); When I have no relationship between Asset and Tracking Device (because it is optional), I can list without problems. But when I modify a tracking device by placing a relation with Asset, it stops working. – Juan Calderon Dec 05 '18 at 17:02
  • A repro would help. Make a [gist](https://gist.github.com/lbargaoanu/9c7233441c3a3413cc2b9b9ebb5964a9) that we can execute and see fail. Without EF. – Lucian Bargaoanu Dec 05 '18 at 20:31
  • I create a gist the link is: https://gist.github.com/juanjinario/f4b2fc97f6784f089b0783653f694551 ; I dont know for what reason the AutoMapper doesnt work, please help me – Juan Calderon Dec 07 '18 at 14:12
  • That's not helpful. If you can make a proper repro (look at my gist), I'll take a look. – Lucian Bargaoanu Dec 07 '18 at 14:22
  • I need more information, what is Proper Repro?, I see your Gist but i dont know the diference with my Gist. I just see a One document with model and Automapper, Do yo need just this? – Juan Calderon Dec 07 '18 at 14:41
  • I want only the AM part, that I can copy paste and execute. – Lucian Bargaoanu Dec 07 '18 at 14:43
  • The problem occurs when the query is executed, the part: var trackingDevices = _appContext.TrackingDevices.Include(td => td.Asset).ToListAsync(); the include part puts an Asset inside of each Tracking Device, and this Asset have a Tracking Device, and the Tracking Device have a Asset recursively, I do not know how to prevent the mapping from being done recursively. – Juan Calderon Dec 07 '18 at 15:27
  • OK, so in fact it's an EF issue. I see. Try lazy loading. – Lucian Bargaoanu Dec 07 '18 at 15:28
  • I dont know, I think that is a AutoMapper problem, please see the statement, In viewModels, AssetViewModel have a TrackingDeviceViewModel property AND TrackingDeviceViewModel have a AssetViewModel property, because I have a ONE-ONE relationship and I need the two Entities in my view, when I apply the Mapper in a Tracking Device ViewModel this have a Asset View Model too and the Asset have a Tracking Device, How I can how can I avoid mapping in infinity or control the depth of the mapping? – Juan Calderon Dec 07 '18 at 15:43
  • I don't think so, but a repro would certainly prove me wrong. – Lucian Bargaoanu Dec 07 '18 at 15:54

0 Answers0