Reputation: 1887
I am using latest version of AutoMapper.
I have Mapper.CreateMap<ArticleDto, EditViewModel>();
in my automapper bootstrapper class which is called in global.asax
Here is the complete listing of same:
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();
}
}
I spent nearly two hours figuring whats wrong with below mappings.
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; }
}
Here the code in my controller.
// Retrive ArticleDto from service
var articleDto = _engine.GetBySlug(slug);
// Convert ArticleDto to ViewModel and send it to view
var viewModel = Mapper.Map<EditViewModel>(articleDto);
this conversion of Dto to ViewModel fails.
Can anyone help me out on this one? Here is the exception detials
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
Here is what Mapper.AssertConfigurationIsValid(); results into
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
As mentioned in above error, here is how i updated my mappings
Mapper.CreateMap<ArticleDto, EditViewModel>()
.ForMember(dest => dest.Categories, opt => opt.Ignore());
Even then i get the same error as given below.
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
Upvotes: 1
Views: 386
Reputation: 26765
Two problems in your code:
The first is what's causing your issue, the second is just something I saw in your config.
Upvotes: 3