代码之家  ›  专栏  ›  技术社区  ›  Jeremie de Vos

在ASP.NET核心中配置automapper

  •  1
  • Jeremie de Vos  · 技术社区  · 7 年前

    我正在尝试使用automapper 8.0.0来填充 WorkViewModel . 此列表正在从 Work 使用实体框架从数据库中类。

    初始化时出现问题的原因是引发以下错误:

    InvalidOperationException:映射器未初始化

    我做错什么了?

    我已经设置了以下代码!

    创业公司

    services.AddAutoMapper();
    

    正在调用的函数:

    public async Task<IEnumerable<WorkViewModel>> GetAllAsync()
    {
        IList<Work> works = await _context.Work.ToListAsync();
    
        IList<WorkViewModel> viewModelList = Mapper.Map<IList<Work>, IList<WorkViewModel>>(works);
    
        return viewModelList;
    }
    

    配置:

    public class MappingProfile : Profile
    {
        public MappingProfile()
        {
            Mapper.Initialize(cfg =>
            {
                cfg.CreateMap<WorkViewModel, Work>();
            });
        }
    }
    

    WorkViewModel:

    public class WorkViewModel
    {
        public int WorkID { get; set; }
        public string Name { get; set; }
        public byte[] Tumbmail { get; set; }
        public string Discription { get; set; }
    
        public string Client { get; set; }
        public DateTime Date { get; set; }
        public string PreviewLink { get; set; }
        public string GitLink { get; set; }
        public string DownloadLink { get; set; }
    
        public int DetailID { get; set; }
        public byte[] Banner { get; set; }
        public string Documentation { get; set; }
    
        public int CategoryID { get; set; }
        public string Category { get; set; }
    }
    

    工作模式:

    public class Work
    {
        [Key]
        public int WorkID { get; set; }
    
        [Display(Name = "Project Name")]
        public string Name { get; set; }
    
        [Display(Name = "Client name")]
        public string Client { get; set; }
    
        [Display(Name = "Description")]
        public string Discription { get; set; }
    
        [Display(Name = "Date")]
        public DateTime Date { get; set; }
    
        [Display(Name = "Thumbmail")]
        public byte[] Tumbmail { get; set; }
    
        [Display(Name = "Preview Link")]
        public string PreviewLink { get; set; }
    
        [Display(Name = "Git Link")]
        public string GitLink { get; set; }
    
        [Display(Name = "DownloadLink")]
        public string DownloadLink { get; set; }
    
    
        public WorkCategory workCategory { get; set; }
        public WorkDetailed WorkDetailed { get; set; }
    }
    
    1 回复  |  直到 7 年前
        1
  •  -1
  •   TanvirArjel    7 年前

    只增加 services.AddAutoMapper(); ConfigureServices 方法不适用于您。您必须配置 AutoMapper 如下:

    public void ConfigureServices(IServiceCollection services)
    {
       // Auto Mapper Configurations
        var mappingConfig = new MapperConfiguration(mc =>
        {
            mc.AddProfile(new MappingProfile());
        });
    
        IMapper mapper = mappingConfig.CreateMapper();
        services.AddSingleton(mapper);
    
        services.AddMvc();
    }
    

    也别忘了安装 AutoMapper.Extensions.Microsoft.DependencyInjection NuGET包。

    推荐文章