I get error when usinng AutoMaper for netcore 2.1 projet
Mapper not initialized. Call Initialize with appropriate configuration. If you are trying to use mapper instances through a container or otherwise, make sure you do not have any calls to the static Mapper.Map methods, and if you're using ProjectTo or UseAsDataSource extension methods, make sure you pass in the appropriate IConfigurationProvider instance. AutoMapper.Mapper.get_Configuration() in Mapper.cs, line 23
I had configed it
public class AutoMapperConfig
{
public static MapperConfiguration RegisterMappings()
{
return new MapperConfiguration(cfg =>
{
cfg.AddProfile(new DomainToViewModelMappingProfile());
cfg.AddProfile(new ViewModelToDomainMappingProfile());
});
}
}
File DomainToViewModelMappingProfile.cs
public class DomainToViewModelMappingProfile : Profile{
public DomainToViewModelMappingProfile(){
CreateMap<Function, FunctionViewModel>();
CreateMap<AppUser, AppUserViewModel>();
CreateMap<AppRole, AppRoleViewModel>();
}
}
File Startup.cs
services.AddSingleton(Mapper.Configuration);
services.AddScoped<IMapper>(sp => new Mapper(sp.GetRequiredService<AutoMapper.IConfigurationProvider>(), sp.GetService));
Anyone can help me? Thanks you!