hii*_*t95 1 c# automapper asp.net-core
将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());
});
}
}
Run Code Online (Sandbox Code Playgroud)
文件DomainToViewModelMappingProfile.cs
public class DomainToViewModelMappingProfile : Profile{
public DomainToViewModelMappingProfile(){
CreateMap<Function, FunctionViewModel>();
CreateMap<AppUser, AppUserViewModel>();
CreateMap<AppRole, AppRoleViewModel>();
}
}
Run Code Online (Sandbox Code Playgroud)
文件启动
services.AddSingleton(Mapper.Configuration);
services.AddScoped<IMapper>(sp => new Mapper(sp.GetRequiredService<AutoMapper.IConfigurationProvider>(), sp.GetService));
Run Code Online (Sandbox Code Playgroud)
有人可以帮助我吗?谢谢!
我建议您为Microsoft-ioc 使用automapper扩展。
在您的configureservice方法(启动类)中:
services.AddAutoMapper(typeof(Startup).Assembly);
Run Code Online (Sandbox Code Playgroud)
然后,您不需要手动配置概要文件,因为扩展名将扫描给定程序集或传递给它的程序集中的类型。
| 归档时间: |
|
| 查看次数: |
5459 次 |
| 最近记录: |