我正在使用最新版本的 AutoMapper。
我 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