Search code examples
c#.net-coreentity-framework-coreautomapper-6

AutoMapping Entity to Model


I tried to map these but I am getting

AutoMapper.AutoMapperMappingException: 'Missing type map configuration or unsupported mapping.'

public class MenuItemViewModel
{
    [Required]
    public string Text { get; set; }
    public string URL { get; set; }
    public string Icon { get; set; }
    public IList<MenuItemViewModel> Children { get; set; }
    public MenuItemViewModel Parent { get; set; }
    public int Sort { get; set; }
}

public class MenuItem
{
    public Guid Id { get; set; }
    public string Text { get; set; }
    public string URL { get; set; }
    public string Icon { get; set; }
    public IList<MenuItem> Children { get; set; }
    public MenuItem Parent { get; set; }
    public bool onMenu { get; set; }
    public int Sort { get; set; }

    public IList<MenuItemPermittedRole> MenuItemPermittedRoles { get; set; }
}

Mapper.Initialize(cfg => {
    cfg.CreateMap<MenuItem, MenuItemViewModel>().ReverseMap();
});

MenuItemViewModel result = Mapper.Map<MenuItemViewModel>(menuItems); ERROR HERE

Solution

  • It's difficult to answer without knowing exactly which error are you receiving, but I can guess... If menuItems is an IEnumerable<MenuItem>, you should write

    var result = Mapper.Map<IEnumerable<MenuItemViewModel>>(menuItems);