Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automapper: "Missing type map configuration or unsupported mapping"

Tags:

c#

automapper

AssertConfigurationIsValid Passes, and the object being tried is fully populated, but I get the error on the first Map request called.

I'm trying to map

Survey ToLoad = Mapper.Map<Survey>(U);

I'm initializing automapper with the code below.

//Lots of other Maps
Mapper.Initialize(cfg => cfg.CreateMap<User, SMUser>()
      .ForMember(t => t.AccountType, s => s.MapFrom(so => so.AccountType != null ? so.AccountType : String.Empty))
      .ForMember(t => t.Username, s => s.MapFrom(so => so.Username != null ? so.Username : String.Empty)));


Mapper.Initialize(cfg => cfg.CreateMap<SurveyMonkey.Containers.Survey, Survey>().ForMember(t => t.AnalyzeUrl, s => s.MapFrom(so => so.AnalyzeUrl != null ? so.AnalyzeUrl : String.Empty))
      .ForMember(t => t.Category, s => s.MapFrom(so => so.Category != null ? so.Category : String.Empty))
      .ForMember(t => t.CollectUrl, s => s.MapFrom(so => so.CollectUrl != null ? so.CollectUrl : String.Empty))
      .ForMember(t => t.EditUrl, s => s.MapFrom(so => so.EditUrl != null ? so.EditUrl : String.Empty))
      .ForMember(t => t.Language, s => s.MapFrom(so => so.Language != null ? so.Language : String.Empty))
      .ForMember(t => t.Preview, s => s.MapFrom(so => so.Preview != null ? so.Preview : String.Empty))
      .ForMember(t => t.SummaryUrl, s => s.MapFrom(so => so.SummaryUrl != null ? so.SummaryUrl : String.Empty))
      .ForMember(t => t.Title, s => s.MapFrom(so => so.Title != null ? so.Title : String.Empty)) 
      //Some more members
);

//LISTS
Mapper.Initialize(cfg => cfg.CreateMap<List<SurveyMonkey.Containers.Collector>, List<Collector>>());

//Lots of other List Maps

I'm using the latest Stable version from Nuget (5.2.0).

like image 722
Steve Wallace Avatar asked Dec 24 '22 23:12

Steve Wallace


1 Answers

Only call Mapper.Initialize once with the whole configuration, or you will overwrite it.

You can wrap the configuration in a class that inherits AutoMapper.Profile:

using AutoMapper;

public class MyAutoMapperProfile : Profile {

    protected override void Configure() {
        CreateMap<User, SMUser>();
        CreateMap<SurveyMonkey.Containers.Survey, Survey>();
        CreateMap<List<SurveyMonkey.Containers.Collector>, List<Collector>>();
    }
}

Then initialize the Mapper using this Profile:

Mapper.Initialize(cfg => {
    cfg.AddProfile<MyAutoMapperProfile>();
    cfg.AddProfile<OtherAutoMapperProfile>();
});

AutoMapper Configuration

like image 92
Georg Patscheider Avatar answered Jan 04 '23 23:01

Georg Patscheider