Steve Wallace Steve Wallace - 4 days ago 5
C# Question

Automapper: "Missing type map configuration or unsupported mapping"

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

//Open a foreach, some validation..
Survey ToLoad = Mapper.Map<Survey>(U);
Survey.Surveys.Add(ToLoad);
};
Survey.SaveChanges();


The opening statement of my main class is

static void Main(string[] args)
{
SurveyContext Survey = new SurveyContext();
Survey.Database.Initialize(true);

// For AutoMapper
Maptyping();


and in Maptyping we have

//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).

thanks

Answer

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

Comments