StackOverflow exception when calling Mapper.Map() method

166 visualizações
Pular para a primeira mensagem não lida

Akim Tahallaiti

não lida,
7 de set. de 2022, 08:27:2507/09/2022
para AutoMapper-users

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 ?

Lucian Bargaoanu

não lida,
9 de set. de 2022, 06:33:1209/09/2022
para AutoMapper-users
Upgrade AM to the latest. If that doesn't work, a repro would help. Make a [gist](https://gist.github.com/lbargaoanu/9c7233441c3a3413cc2b9b9ebb5964a9) that we can execute and see fail.
Responder a todos
Responder ao autor
Encaminhar
0 nova mensagem