AutoMapper Mapping to Class With Custom Constructor - gecko-8/devwiki GitHub Wiki

Up

When mapping to a class with a custom constructor with parameters (whether or not you've defined a parameter-less constructor as well), AutoMapper will not map properties correctly unless you specify the constructor behaviour. There are basically two options:

Tell it to use the parameterless constructor (if you have one)

CreateMap<SourceType, DestinationType>()
    .ConstructUsing(src => new DestinationType())  // Ignores custom constructor and maps normally
    ... Any other mapping instructions

Tell it to use the custom constructor

CreateMap<SourceType, DestinationType>()
    .ConstructUsing(src => 
        new DestinationType(src.Parameter1, src.Parameter2, ...));