This question already has answers here:
Automapper error saying mapper not initialized
(2个答案)
7个月前关闭。
将NetMap 2.1 Projet用于AutoMaper时出现错误
映射器未初始化。用适当的配置调用初始化。如果您尝试通过容器或其他方式使用mapper实例,请确保没有对静态Mapper.Map方法的任何调用,并且如果您使用的是ProjectTo或UseAsDataSource扩展方法,请确保传递适当的IConfigurationProvider实例。
Mapper.cs中的AutoMapper.Mapper.get_Configuration(),第23行
我已经配置好了
文件DomainToViewModelMappingProfile.cs
文件启动
有人可以帮助我吗?谢谢!
然后,您无需手动配置概要文件,因为扩展名将扫描给定程序集或传递给它的程序集中的类型。
(2个答案)
7个月前关闭。
将NetMap 2.1 Projet用于AutoMaper时出现错误
映射器未初始化。用适当的配置调用初始化。如果您尝试通过容器或其他方式使用mapper实例,请确保没有对静态Mapper.Map方法的任何调用,并且如果您使用的是ProjectTo或UseAsDataSource扩展方法,请确保传递适当的IConfigurationProvider实例。
Mapper.cs中的AutoMapper.Mapper.get_Configuration(),第23行
我已经配置好了
public class AutoMapperConfig
{
public static MapperConfiguration RegisterMappings()
{
return new MapperConfiguration(cfg =>
{
cfg.AddProfile(new DomainToViewModelMappingProfile());
cfg.AddProfile(new ViewModelToDomainMappingProfile());
});
}
}
文件DomainToViewModelMappingProfile.cs
public class DomainToViewModelMappingProfile : Profile{
public DomainToViewModelMappingProfile(){
CreateMap<Function, FunctionViewModel>();
CreateMap<AppUser, AppUserViewModel>();
CreateMap<AppRole, AppRoleViewModel>();
}
}
文件启动
services.AddSingleton(Mapper.Configuration);
services.AddScoped<IMapper>(sp => new Mapper(sp.GetRequiredService<AutoMapper.IConfigurationProvider>(), sp.GetService));
有人可以帮助我吗?谢谢!
最佳答案
我建议您对Microsoft-ioc使用automapper extension。
在您的configureservice方法(启动类)中:
services.AddAutoMapper(typeof(Startup).Assembly);
然后,您无需手动配置概要文件,因为扩展名将扫描给定程序集或传递给它的程序集中的类型。
关于c# - 映射器未初始化。使用适当的配置调用Initialize ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51625446/
10-17 02:00