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);
You can use this with AutoMapper 5.2.
Your Profile class as below
Then in your Global.asax
Now when you need to create an instance
Hope this helps.
Do you actually need to use a Profile in this scenario? If you don't, you can try just initialising the Mapper like this:
However, if you would like to still register a Profile, you can do this:
Hope this helps. If you're using AutoMapper 5.0, remember that this is still at beta-1 at this point in time.