Problem with AutoMapper relationship many to many C #

1

I have two classes with a many-to-many relationship, all the mapping is ready, the insert is working, but when I try to do AutoMapper it goes into an infinite loop.

Workflow:

    public class Workflow : Elemento
    {
        public IList<EventoWorkflow> EventosWorkflow;
    }

EventWorkflow:


    public class EventoWorkflow : Evento
    {
        public IList<Workflow> Workflows;
    }

I tried several things that I found on the internet to try to make it work but none of them worked, the last attempt I made was like this:

Workflow - > WorkflowDTO


Mapper.CreateMap<Workflow, WorkflowDTO>()
                .ForMember(x => x.EventosWorkflow, m => m.MapFrom(w => w.EventosWorkflow.Select(y => y.ActualObject).ToList()));

EventWorkflow - > EventWorkflowDTO


    Mapper.CreateMap<EventoWorkflow, EventoWorkflowDTO>()
                    .ConstructUsing(x => new EventoWorkflowDTO(x.CodigoEvento, AutoMapper.Mapper.Map<IList<Workflow>, List<WorkflowDTO>>(x.Workflows)));

But he still gives the error:

  

An unhandled exception of type 'System.StackOverflowException'

Does anyone know how I can solve this problem?

    
asked by anonymous 21.01.2016 / 17:32

1 answer

0

Just give .Ignore() in related classes to stop the infinite loop. This will make the relationships come with null , so you should work with the data in this way.

    
20.05.2016 / 16:55