无法将 Dto 映射到 ViewModel

Not able to map Dto to ViewModel

我正在使用最新版本的 AutoMapper。

我的自动映射器引导程序 class 中有 Mapper.CreateMap<ArticleDto, EditViewModel>();,它在 global.asax

中调用

这是相同的完整列表:

public static class AutoMapperConfig
    {
        public static void Configure()
        {
            Mapper.Initialize(cfg => cfg.AddProfile(new AutomapperWebConfigurationProfile()));
            Mapper.Initialize(cfg => cfg.AddProfile(new AutomapperServiceConfigurationProfile()));
        }
    }

    public class AutomapperWebConfigurationProfile : Profile
    {
        protected override void Configure()
        {

            Mapper.CreateMap<CreateArticleViewModel, ArticleDto>()
                .ForMember(dest => dest.Title, opts => opts.MapFrom(src => src.Title.Trim()))
                .ForMember(dest => dest.PostBody, opts => opts.MapFrom(src => src.PostBody.Trim()))
                .ForMember(dest => dest.Slug,
                    opts =>
                        opts.MapFrom(
                            src => string.IsNullOrWhiteSpace(src.Slug) ? src.Title.ToUrlSlug() : src.Slug.ToUrlSlug()))
                .ForMember(dest => dest.Id, opt => opt.Ignore())
                .ForMember(dest => dest.CreatedOn, opt => opt.Ignore())
                .ForMember(dest => dest.Author, opt => opt.Ignore());


            Mapper.CreateMap<ArticleDto, EditViewModel>()
                .ForMember(dest => dest.Categories, opt => opt.Ignore());



             Mapper.AssertConfigurationIsValid();
        }
    }

我花了将近两个小时来弄清楚下面的映射有什么问题。

 public class ArticleDto
    {
        public int Id { get; set; }
        public string Slug { get; set; }
        public string Title { get; set; }
        public string PostBody { get; set; }
        public DateTime CreatedOn { get; set; }
        public bool IsPublished { get; set; }
        public string Author { get; set; }
        public List<string> ArticleCategories { get; set; }
    }



 public class EditViewModel
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string PostBody { get; set; }
        public DateTime CreatedOn { get; set; }
        public string Slug { get; set; }
        public bool IsPublished { get; set; }
        public IEnumerable<SelectListItem> Categories { get; set; }
        public List<string> ArticleCategories { get; set; }
    }

这是我控制器中的代码。

// Retrive ArticleDto  from service
var articleDto = _engine.GetBySlug(slug);

// Convert ArticleDto to ViewModel and send it to view
var viewModel = Mapper.Map<EditViewModel>(articleDto);

此 Dto 到 ViewModel 的转换失败。

谁能帮我解决这个问题?这是异常细节

Exception Details: AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.

Mapping types:
ArticleDto -> EditViewModel
NoobEngine.Dto.ArticleDto -> NoobEngineBlog.ViewModels.Article.EditViewModel

Destination path:
EditViewModel

Source value:
NoobEngine.Dto.ArticleDto

这是 Mapper.AssertConfigurationIsValid();结果为

Unmapped members were found. Review the types and members below.
Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
=============================================================================
ArticleDto -> EditViewModel (Destination member list)
NoobEngine.Dto.ArticleDto -> NoobEngineBlog.ViewModels.Article.EditViewModel (Destination member list)

Unmapped properties:
Categories

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: AutoMapper.AutoMapperConfigurationException: 
Unmapped members were found. Review the types and members below.
Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
=============================================================================
ArticleDto -> EditViewModel (Destination member list)
NoobEngine.Dto.ArticleDto -> NoobEngineBlog.ViewModels.Article.EditViewModel (Destination member list)

Unmapped properties:
Categories

如上述错误所述,这是我更新映射的方式

  Mapper.CreateMap<ArticleDto, EditViewModel>()
                .ForMember(dest => dest.Categories, opt => opt.Ignore());

即便如此,我仍会收到如下所示的相同错误。

An exception of type 'AutoMapper.AutoMapperMappingException' occurred in AutoMapper.dll but was not handled in user code

Missing type map configuration or unsupported mapping.

Mapping types:
ArticleDto -> EditViewModel
NoobEngine.Dto.ArticleDto -> NoobEngineBlog.ViewModels.Article.EditViewModel

Destination path:
EditViewModel

Source value:
NoobEngine.Dto.ArticleDto

您的代码中有两个问题:

  1. 不要调用 Initialize 两次。初始化重置配置。
  2. 在您的配置文件中调用基本的 CreateMap 方法,而不是 Mapper.CreateMap

第一个是导致你的问题的原因,第二个是我在你的配置中看到的。