我已阅读所有我可以在网上找到有关此问题的信息,但没有任何帮助。这里是我的代码:Automapper 5.0.2 - 遗漏型地图配置或不支持的映射
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是给我的错误:
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: }
感谢您的回复。我实际上已经认识到这一点,但它并没有解决我的问题。我将Automapper v 2升级到v5,它需要修改一些代码才能工作。在我的情况下,它不能自动处理对象内的集合。我不得不添加一行代码来指定: – Mao
谢谢!这一直让我疯狂的整个早晨...我们有多个Mapper.Initialize实例,一旦我们将它们合并成一个,配置文件,一切正常。 –