Automapper 5.0.2 - Missing Type Map Configuration or Unsupported Display

I read everything I could find on the Internet on this issue, but nothing helps. 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 gives me an error:

Missing type map configuration or unsupported display.

Display types:
    User → UserListViewModel
    Model.Models.User → Model.ViewModels.UserListViewModel
      Description: An unhandled exception occurred during the execution of the current web request. View the stack trace for more information about the error and its occurrence in the code.

Exception Details: AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.

:
→ UserListViewModel
Model.Models.User → Model.ViewModels.UserListViewModel

:

45:
    46: var users = _repo.AllIncluding(u = > u.Roles, u = > u.CostCentres).OrderBy(u = > u.UserName).ToList();     47: var model = Mapper.Map, List > ();
    48: ;
   49:}

+4
2

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

. , , . 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));
        });
0

Source: https://habr.com/ru/post/1649206/


All Articles