AutoMapper configuration in older and newer versions

Recently when I was looking for various architecture approached and tools sets, I saw this post which is very useful. But while I was going through the post I was getting error while implementing AutoMapper. I started getting below errors when I tried to configure AutoMapper as given on the link.

  • The type or namespace name ‘Profile’ could not be found (are you missing a using directive or an assembly reference?)
  • ‘ViewModelToDomainMappingProfile.ProfileName’: no suitable method found to override
  • ‘ViewModelToDomainMappingProfile.Configure()’: no suitable method found to override
  • ‘DomainToViewModelMappingProfile.Configure()’: no suitable method found to override
  • ‘Mapper’ does not contain a definition for ‘CreateMap’
  • ‘Mapper’ does not contain a definition for ‘CreateMap’

After some time I figured out the problem. Actually while following the tutorials I installed latest version (7.0.1 at that time) of AutoMapper from nuget while this tutorial was using 3.3.1 (that article was written sometime in 2015).

So to solve this problem there are two methods

  • Install older version of auto mapper 3.3.1 – 4.0.4. It uses same old approach which this tutorial is explaining
  • Install latest version with and use below approach for configuring mapping
Mapper.Initialize(cfg => {
    cfg.CreateMap<User, UserViewModel>();
});

Mapper.Initialize(cfg => {
    cfg.CreateMap<User, UserViewModel>()
        .ForMember(vm => vm.UserName, map => map.MapFrom(dm => dm.UserId));
});