Map two identical models, nested with automapper

290 Views Asked by At

I did some research but I couldn't find exactly what I wanted.

I have an endless menu. I have a MenuDTO and a MenuViewModel that I use for this menu. I had no problem matching between model and DTO, but am having trouble mapping DTO to ViewModel. Obviously I couldn't find the solution, can you help?

My MenuDTO Object

    public class MenuDto : BaseDto
    {
        public string Name { get; set; }
        public string Icon { get; set; }
        public string Order { get; set; }
        public string Url { get; set; }
        public bool IsVisible { get; set; }
        public int ParentId { get; set; }
        public MenuDto ParentMenu { get; set; }
        public List<MenuDto> Menus { get; set; }
    }

And MenuViewModel

    public class MenuViewModel
    {
        public int Id { get; set; }        
        public bool IsActive { get; set; }
        public string Name { get; set; }
        public string Icon { get; set; }
        public string Order { get; set; }
        public string Url { get; set; }
        public bool IsVisible { get; set; }
        public int ParentId { get; set; }
        public MenuViewModel ParentMenu { get; set; }
        public List<MenuViewModel> Menus { get; set; }
    }

This is how I mapped the MenuDTO and MenuViewModel objects.

    public class WebProfile : Profile
    {
        public WebProfile()
        {
            CreateMap<MenuDto, MenuViewModel>();
            CreateMap<MenuViewModel, MenuDto>();
        }
    }

I call this way in the controller

var navMenuItems = _mapper.Map<List<MenuViewModel>(_menuService.GetNavMenus());

Although all fields are mapped, I get an error on the Menus field.

The error message I get is;

AutoMapperMappingException: Missing type map configuration or unsupported mapping.

Mapping types:
MenuDto -> MenuViewModel
BiPortal2020.Business.ServiceDTOs.Menu.MenuDto -> BiPortal2020.WebUI.Areas.Admin.Models.Menu.MenuViewModel
lambda_method(Closure , MenuDto , MenuViewModel , ResolutionContext )

AutoMapperMappingException: Error mapping types.

Mapping types:
Object -> List`1
System.Object -> System.Collections.Generic.List`1
1

There are 1 best solutions below

1
On BEST ANSWER

The error message implies - AutoMapper, either cannot map between MenuDto and MenuViewModel, or it cannot locate the defined mappings.

I've tested your mappings and they are totally fine. So, what possibility remains is AutoMapper cannot locate your mappings.

I'm Assuming the Business Layer and UI Layer you mentioned in the comment section are two separate projects. Since the WebProfile is defined in the UI Layer, you have to tell AutoMapper that it should search that assembly to find the mappings. Since your mappings between Models and DTOs are working, I can guess you've already done the same for BusinessProfile which is defined in the Business Layer.

I don't know about your existing code, but you could do something like this - in the Startup.Configure method add/modify the following line -

services.AddAutoMapper(typeof(IDtoMapping), typeof(IViewModelMapping));

where IDtoMapping and IViewModelMapping are two marker interface (empty interface, used only to identify the assembly they are declared in) declared in the Business Layer and UI Layer, respectively.