I am using the code below in AutoMapperConfig.cs
in App_Start
folder. I
initialized it in Global.asax
as AutoMapperConfiguration.Configure()
But I am unable to use the Mapper.Map<Hospital, MongoHospital>
in my
controller. It is throwing an exception that no mappings are defined. It
was working in previous versions of Automapper which were supporting
Mapper.CreateMap<>
methods. I am confused how to use
MapperConfiguration
instance.
public static class AutoMapperConfiguration
{
public static void Configure()
{
Mapper.Initialize(
cfg =>
{
cfg.AddProfile<HospitalProfile>();
}
);
Mapper.AssertConfigurationIsValid();
}
}
public class HospitalProfile : Profile
{
protected override void Configure()
{
var config = new MapperConfiguration(
cfg =>
{
cfg.CreateMap<Hospital, MongoHospital>()
.ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id.ToString()));
});
config.CreateMapper();
}
}
Trying to access this map as below
Mapper.Map<IEnumerable<Hospital>, IEnumerable<MongoHospital>>(hospitalsOnDB);