Automapper map recursive menu tree

2.6k Views Asked by At

I followed the instructions on https://www.mikesdotnetting.com/article/255/entity-framework-recipe-hierarchical-data-management

Now I'm trying to map my entities to my dtos which is constantly failing.

I saw many issues releated to this but none of them has a "clean" way of mapping. I started like this...

CreateMap<MenuItem, DTO.MenuItem>()
    .ForMember(d => d.Children, opt => opt.MapFrom(src => src))
    .ForMember(d => d.Parent, opt => opt.MapFrom(src => src.Parent))
    .ForMember(d => d.MenuText, opt => opt.MapFrom(src => src.MenuText))
    .ForMember(d => d.LinkUrl, opt => opt.MapFrom(src => src.LinkUrl))
    .ForMember(d => d.MenuOrder, opt => opt.MapFrom(src => src.MenuOrder))
    .ForMember(d => d.ParentMenuItemId, opt => opt.MapFrom(src => src.ParentMenuItemId))
    .ForMember(d => d.MenuId, opt => opt.MapFrom(src => src.MenuId))
    .ForMember(d => d.MenuItemId, opt => opt.MapFrom(src => src.MenuItemId))
    .ForMember(d => d.Menu, opt => opt.MapFrom(src => src.Menu));

How should the mapper know which parent element to map to?

Any help would be appreciated.

1

There are 1 best solutions below

0
On