I use AutoMapper V.6.1.1 as a mapper in my ASP.Net project. Before I had configuration as below:
 var config = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<A, B>();
            cfg.CreateMap<C, D>().ForMember(dest => dest.CityDesc, opt => opt.MapFrom(src => src.City));
        });
  var mapper = config.CreateMapper();
  var var1= mapper.Map<B>(request);
  var var2= mapper.Map<List<C>, List<D>>(result);
Now, I want to refactor the code, using Mapper.Initialize(). So I used:
 Mapper.Initialize(cfg =>
        {
            cfg.CreateMap<A, B>();
            cfg.CreateMap<C, D>().ForMember(dest => dest.CityDesc, opt => opt.MapFrom(src => src.City));
        });
  var var1= Mapper.Map<B>(request);
  var var2= Mapper.Map<List<C>, List<D>>(result);
I have an run time error:
 Missing type map configuration or unsupported mapping. Mapping types:    A-> B
Is there any problem with using multiple configurations in Mapper.Initialize? There is no error in the case that has one mapping in Initialize() body. How should I fix the error? 
Maybe you have more than one Mapper.Initialize in your project while you should not have multiple Mapper.Initialize in your project else it will become override and you lost previous mapping configurations that you set by Mapper.Initialize. Now It is possible to get the error (Missing type map configuration or unsupported mapping.)
I recommend you to use AutoMapper.Profile. You can warp your mapping configurations in the form of grouped (in separated Profiles) then register all of theme by Mapper.Initialize at once ;)
Look at this example:
public class AB_Profile : Profile {
    protected override void Configure() {
        CreateMap<A, B>();
     // CreateMap<A, B1>();
     // CreateMap<A, B2>();
    }
}
public class CD_Profile : Profile {
    protected override void Configure() {
        CreateMap<C, D>()
        .ForMember(dest => dest.CityDesc, opt => opt.MapFrom(src => src.City));
    }
}
Then initialize the Mapper using above Profiles:
Mapper.Initialize(cfg => {
    cfg.AddProfile<AB_Profile >();
    cfg.AddProfile<CD_Profile >();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With