Search code examples
c#automapperautomapper-9

I keep getting "needs to have a constructor with 0 args or only optional args. (Parameter 'type')"


I am using version 9. I am using a Profile based configuration. When I run the application the Mapper.Map<>() method throws the following exception:

JobAssist.Services.ResumeBankMgmt.API.Application.ViewModels.ResumeBankViewModel needs to have a constructor with 0 args or only optional args. (Parameter 'type')

I don't know what is causing this.


Solution

  • Classic mapping works for mapping based on source members: https://docs.automapper.org/en/latest/Construction.html

    If you cannot change the class (in contracts for example) having constructors with parameters that are different from members, you can use ConstructUsing.

    You can use it like this to specify arguments :

    Mapper.CreateMap<ObjectFrom, ObjectTo>()
        .ConstructUsing(x => new ObjectTo(arg0, arg1, etc));
    

    Check the full answer here: Automapper - how to map to constructor parameters instead of property setters