Reputation: 41
I've read all I could find online about this issue but nothing is helping. Here is my code:
Mapper.Initialize(cfg =>
{
cfg.CreateMap<User, UserListViewModel>()
.ForMember("RoleNames", c => c.Ignore())
.ForMember("CostCentreNames", c => c.Ignore())
.ForMember("RollupGroupNames", c => c.Ignore())
.ForMember(c => c.CostCentres, m => m.MapFrom(d => d.DetailCostCentres))
;
});
Mapper.Initialize(cfg =>
{
cfg.CreateMap<CostCentre, CostCentreListViewModel>();
});
var users = _repo.AllIncluding(u => u.Roles, u=>u.CostCentres).OrderBy(u => u.UserName).ToList();
var model = Mapper.Map<List<User>, List<UserListViewModel>>(users);
Mapper.Map is giving me the error:
Missing type map configuration or unsupported mapping.
Mapping types:
User -> UserListViewModel
Model.Models.User -> Model.ViewModels.UserListViewModel
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.AutoMapperMappingException: Missing type map configuration or unsupported mapping.
Mapping types:
User -> UserListViewModel
Model.Models.User -> Model.ViewModels.UserListViewModelSource Error:
Line 45:
Line 46: var users = _repo.AllIncluding(u => u.Roles, u=>u.CostCentres).OrderBy(u => u.UserName).ToList();
Line 47: var model = Mapper.Map, List>(users);
Line 48: return model;
Line 49: }
Upvotes: 4
Views: 2019
Reputation: 41
Thanks for your reply. I actually figured that one out but, it did not solve my issue. I upgraded Automapper v 2 to v5 and it needed some code modifications to work. in my case, it couldn't automatically handle the collections inside the object. I had to add a line of code to specify that:
Mapper.Initialize(cfg =>
{
cfg.CreateMap<User, UserListViewModel>()
.ForMember("RoleNames", c => c.Ignore())
.ForMember("CostCentreNames", c => c.Ignore())
.ForMember("RollupGroupNames", c => c.Ignore())
.ForMember(c => c.CostCentres, m => m.MapFrom(d => d.DetailCostCentres));
cfg.CreateMap(typeof(Role), typeof(RoleViewModel));
cfg.CreateMap(typeof(CostCentre), typeof(CostCentreListViewModel));
});
Upvotes: 0
Reputation: 109080
You can do only one Mapper.Initialize
call:
Mapper.Initialize(cfg =>
{
cfg.CreateMap<User, UserListViewModel>()
.ForMember("RoleNames", c => c.Ignore())
.ForMember("CostCentreNames", c => c.Ignore())
.ForMember("RollupGroupNames", c => c.Ignore())
.ForMember(c => c.CostCentres, m => m.MapFrom(d => d.DetailCostCentres));
cfg.CreateMap<CostCentre, CostCentreListViewModel>();
});
Upvotes: 5