I get a stack overflow for the following display:
Mapper.CreateMap<Parent, ParentViewModel>() .ForMember(x => x.Children, o => o.MapFrom(x => x.Children.ConvertToChildrenViewModel())); Mapper.CreateMap<Children, ChildrenViewModel>() .ForMember(x => x.Parents, o => o.MapFrom(x => x.Parents.ConvertToParentViewModel()));
I understand why this is happening, obviously an endless loop here. How should I make this work in automapper? I need parents to know about their children and their children in order to find out about their parents. Should I create another ViewModel
for Children.Parents
that does not contain the Parents.Children
property?
An example of an extension method, similar for children:
public static IList<ParentViewModel> ConvertToParentViewModel(this IEnumerable<Parent> parents) { return Mapper.Map<IList<ParentViewModel>>(parents); }
source share