2016-07-26 98 views
4

我已阅读所有我可以在网上找到有关此问题的信息,但没有任何帮助。这里是我的代码: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.UserListViewModel

Source 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: }

回答

5

您只能做一Mapper.Initialize电话:

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>(); 
     }); 
+0

感谢您的回复。我实际上已经认识到这一点,但它并没有解决我的问题。我将Automapper v 2升级到v5,它需要修改一些代码才能工作。在我的情况下,它不能自动处理对象内的集合。我不得不添加一行代码来指定: – Mao

+0

谢谢!这一直让我疯狂的整个早晨...我们有多个Mapper.Initialize实例,一旦我们将它们合并成一个,配置文件,一切正常。 –

0

感谢您的答复。我实际上已经认识到这一点,但它并没有解决我的问题。我将Automapper v 2升级到v5,它需要修改一些代码才能工作。在我的情况下,它不能自动处理对象内的集合。我不得不添加一行代码来指定:

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)); 
     }); 
相关问题