AutoMapper配置异常

时间:2018-04-12 09:40:14

标签: asp.net-mvc .net-core automapper

我有一个模型类

public class Brochure
    {
        [Key]
        public int BrochureId { get; set; }
        public string Name { get; set; }
        public string TypeOfCover { get; set; }
        public int NumberOfPages { get; set; }
        public LibraryType Type { get; set; }
    }

和查看模型

public class BrochureView
    {
        public int BrochureId { get; set; }
        public string Name { get; set; }
        public string TypeOfCover { get; set; }
        public int NumberOfPages { get; set; }
    }

在宣传册服务中我正在映射模型和视图模型

public class BrochureService
    {
        private BrochureRepository _brochureRepository;

        public BrochureService(DbContextOptions<LibraryContext> options)
        {
            _brochureRepository = new BrochureRepository(options);
        }

        public void Create(BrochureView brochureView)
        {
            var brochure = Mapper.Map<BrochureView, Brochure>(brochureView);
            brochure.Type = LibraryType.Brochures;

            _brochureRepository.Create(brochure);
        }
    }

这是我的automapper探查器:

public static class AutoMapperProfile
    {
        public static void RegisterMappings()
        {
            Mapper.Initialize(cfg =>
            {
                cfg.CreateMap<Brochure, BrochureView>();

                cfg.CreateMap<BrochureView, Brochure>()
                .ForMember(src => src.Type, opt => opt.Ignore());

            });
        }
    }

但我有一个例外: enter image description here

找到未映射的成员。 AutoMapper无法映射“类型”属性。我该如何解决这个问题?

要回答评论中的问题,那就是整个MappingProfile

public static class AutoMapperProfile
    {
        public static void RegisterMappings()
        {
            Mapper.Initialize(cfg =>
            {
                cfg.CreateMap<Book, BookView>();
                cfg.CreateMap<BookView, Book>();

                cfg.CreateMap<Magazine, MagazineView>();
                cfg.CreateMap<MagazineView, Magazine>();

                cfg.CreateMap<Brochure, BrochureView>();
                cfg.CreateMap<BrochureView, Brochure>()
                    .ForMember(src => src.Type, opt => opt.Ignore());


                cfg.CreateMap<PublicHouse, PublicHouseView>();
                cfg.CreateMap<PublicHouseView, PublicHouse>();

                cfg.CreateMap<Book, LibraryView>()
               .ForMember(dest => dest.Name, opt => opt.ResolveUsing(src => src.Name))
               .ForMember(dest => dest.Type, opt => opt.ResolveUsing(src => (LibraryType)src.Type));

                cfg.CreateMap<Brochure, LibraryView>()
              .ForMember(dest => dest.Name, opt => opt.ResolveUsing(src => src.Name))
              .ForMember(dest => dest.Type, opt => opt.ResolveUsing(src => (LibraryType)src.Type));

                cfg.CreateMap<Magazine, LibraryView>()
              .ForMember(dest => dest.Name, opt => opt.ResolveUsing(src => src.Name))
              .ForMember(dest => dest.Type, opt => opt.ResolveUsing(src => (LibraryType)src.Type));
            });
        }
    }

0 个答案:

没有答案