I know this might be flagged as a duplicate question, and yes, I did review the questions that may already have answered my question, but still couldn't find an answer.
As most of the other questions relate to, my issue is related to models that reference each other. I am well aware of other approaches that I can follow, like refactoring my code to create separate DTO objects for the children. Problem is, there are quite allot of models and refactoring everything will be very cumbersome.
I read the Automapper v5.0 Upgrade Guide, which states the following:
Which I've tried - for example the ClientModel
:
var clientModelMap = CreateMap<Client, ClientModel>()
.ForMember(dest => dest.Id, source => source.MapFrom(x => x.ClientId))
.ForMember(dest => dest.InvoicePayments, source => source.Ignore());
clientModelMap.MaxDepth(1);
clientModelMap.PreserveReferences();
as well as in the Project
model:
var projectModelMap = CreateMap<Project, ProjectModel>()
.ForMember(dest => dest.Client, source => source.Ignore())
.ForMember(dest => dest.Id, source => source.MapFrom(x => x.ProjectId))
.ForMember(dest => dest.ClientName, source => source.MapFrom(x => x.Client.ClientName))
.ForMember(dest => dest.ClientTariff, source => source.MapFrom(x => x.Client.Tariff))
;
projectModelMap.MaxDepth(1);
projectModelMap.PreserveReferences();
The structure is as follows:
- Client
- Project (each client has many projects)
- Resource (each project has a project manager)
- Projects (is associated with one or more projects he/she is working on)
- Client (is associated with a client / acts as the manager)
- Resource (each project has a project manager)
- Project (each client has many projects)
But this has no effect, and I still receive a StackOverflowException
. What am I missing or doing wrong?
I'm open for suggestions, if this is not the way to go.
Many thanks!