Hello Jimmy,
I have a StackOverflow exception when executing following statement :
var models = _mapper.Map<List<ReleaseModel>>(entities);
As you can imagine “entities” are returned by a repository which is relying on EntityFramework Core to fetch data from the DB.
Please find below the different parts of the code which are relevant to explain the context.
Of course - for the sake of clarity- I have removed all unnecessary lines of code.
DTO classes :
public class Raxa
{
...
public decimal? StartReleaseId { get; set; }
public Release StartRelease { get; set; }
public decimal? EndReleaseId { get; set; }
public Release EndRelease { get; set; }
...
}
public class Release
{
public ICollection<Raxa> StartReleaseRaxas { get; set; }
public ICollection<Raxa> EndReleaseRaxas { get; set; }
}
POCO classes :
public class RaxaModel
{
…
public ReleaseModel StartReleaseModel { get; set; }
public ReleaseModel EndReleaseModel { get; set; }
…
}
public class ReleaseModel
{
…
public List<RaxaModel> StartRaxaModels { get; set; }
public List<RaxaModel> EndRaxaModels{ get; set; }
…
}
Mapping class :
public class FromDomainToModel : Profile
{
public FromDomainToModel()
{
...
CreateMap<Release, ReleaseModel>()
.ForMember(t => t.StartRaxaModels, mce => mce.MapFrom(s => s.StartReleaseRaxas))
.ForMember(t => t.EndRaxaModels, mce => mce.MapFrom(s => s.EndReleaseRaxas));
CreateMap<Raxa, RaxaModel>()
.ForMember(t => t.StartReleaseModel, mce => mce.MapFrom(s => s.StartRelease))
.ForMember(t => t.EndReleaseModel, mce => mce.MapFrom(s => s.EndRelease));
...
}
}
I guess this issue might be related to self-referencing, but I do not know how to restructure my code to solve this exception.
Can you please kindly help when you have time ?