1

I'm trying to use Automapper to flatten a Entityframework object that has a nested complex property to a few various models that all inherit the same common properties from a base class, a simplified version of this is as follows:

public class EFObject {
    public int Id { get; set; }
    public int NestedId { get; set; }
    public virtual AnotherModel AnotherModel { get; set; }
}

public class AModel : Model {
    public int AModelId { get; set; }
}

public class BModel : Model {
    public int BModelId { get; set; }
}

As both AModel and BModel share a common base (Model) ideally I would like to declare a common map like so:

mapper.Map<AnotherModel, Model>();

And then when mapping EFObject to AModel I would map the AModelId property within

mapper.Map<EFObject, AModel>();

and then include the AnotherModel -> Model map so that I wouldn't have to map the common properties multiple times (once for AModel and again for BModel, etc).

I was able to achieve the above by using an AfterMap, i.e.

CreateMap<EFObject, AModel>()
   .AfterMap((src, dest) => Mapper.Map(src.AnotherModel, dest))
   .ReverseMap();

However there is a fairly major issue with this solution in that it relies on the static instance of Automapper, and as I am dependency injecting an instance of Automapper, my unit tests that use this map are all failing due to the static Mapper not being instantiated.

I could get around this problem by initializing the static instance in my unit test, but as it requires knowledge of how your maps are structured, it defeats the aim of using Automapper (I feel).

I also know that I could write a converter for each Model, however that isn't ideal as it produces a lot of additional code for something I feel is supported but I'm struggling to find the answer for.

Any ideas on the best way to structure my maps to get around this problem?

  • Use [this](https://github.com/AutoMapper/AutoMapper/blob/08fa99f3ec8d2c3a1b312d6b76d6c42eced7bac2/src/AutoMapper/IMappingExpression.cs#L356) and ```context.Mapper```. – Lucian Bargaoanu May 11 '18 at 10:03
  • @Lucian Bargaoanu Just tried the following: `CreateMap() .AfterMap((src, dest, context) => context.Mapper.Map(src.AnotherModel, dest)) .ReverseMap();` and sadly the result is that it errors stating that the mapper isn't been initialized, which leads me to believe it is attempting to use the static mapper (not the injected one). – user3564855 May 11 '18 at 10:51
  • Context.Mapper is the mapper instance that you called Map on. – Lucian Bargaoanu May 11 '18 at 11:09
  • Thanks again for the response. I've done some digging on my end and it appears your solution does work, but only when mapping directly from the injected mapper instance, not through the ProjectTo extension. Thinking logically I'm guessing this likely stems from ProjectTo using the static instance, is there any way to force it to use the correct instance? – user3564855 May 11 '18 at 11:11
  • See [this](https://github.com/AutoMapper/AutoMapper/blob/c4a091badf28b5851f340998e00f9aa84ad3d322/src/IntegrationTests/ChildClassTests.cs#L105). – Lucian Bargaoanu May 11 '18 at 11:39
  • Thanks for your patience with me. Although passing through the mapper configuration to the ProjectTo method no longer produces any errors, the end result is that the map declared as AfterMap doesn't actually appear to do anything. If I switch out the ProjectTo(_mapper.ConfigurationProvider) with _mapper.Map(efObject) it works fine. Maybe a bug/limitation with the ProjectTo implementation? – user3564855 May 11 '18 at 12:06
  • http://docs.automapper.org/en/stable/Queryable-Extensions.html#supported-mapping-options – Lucian Bargaoanu May 11 '18 at 12:26
  • That explains it, I won't take up anymore of your time. Thanks very much for your help! – user3564855 May 11 '18 at 12:28

0 Answers0