1

i found many posts explain how to resolve the same issue that i have, but not in my case :

so i have 4 classes :

Context Classes : Client, Rerservation. Other classes : ClientDto, ReservationDto.

i added this line to make relationship between classes

Mapper.CreateMap<Client, ClientDto>();
Mapper.CreateMap<Reservation, ReservationDto>();
Mapper.CreateMap<ClientDto, Client>();
Mapper.CreateMap<ReservationDto, Reservation>(); 

Client DTO Classes:

public class ClientDto
{
    public int Id { get; set; }
    ...
    public virtual ICollection<ReservationDto> Reservations { get; set; }
}

Reservation DTO classe :

public class ReservationDto
{
    public int Id { get; set; }
    ...
    public virtual ClientDto Client{ get; set; }
}

So when i would to get list of Client from database :

public IEnumerable<ClientDto> GetClients(Expression<Func<ClientDto, bool>> expression, int count)
{
    return _context.Clients.Project().To<ClientDto>().Where(expression.Expand());
}

i got this error message :

An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll

if i remove reservation relationsShip from ClientDto classe, it's work fine !!

RelationShip :

enter image description here

AHmedRef
  • 2,555
  • 12
  • 43
  • 75
  • Check this question http://stackoverflow.com/questions/22146736/entity-framework-navigation-property for more more information. – Jones Dec 12 '14 at 11:44
  • Thank's , but this cannot resolve my issue – AHmedRef Dec 12 '14 at 11:55
  • 2
    You have a cyclic relationship which results in an infinite object graph which you obviously can't fully map to DTOs. Do you need both `Reservations` and `Client` on your DTOs, removing one or the other would solve this issue. – Ben Robinson Dec 12 '14 at 12:09
  • i checked my code o, have juste one relation between client and reservation please see my post again i changed it ! – AHmedRef Dec 12 '14 at 12:20
  • if i remove for example Client object from reservation, i will not extract client from reservation object – AHmedRef Dec 12 '14 at 12:27

1 Answers1

2

As @Ben suggested, you have a circular reference: ClientDto contains a collection of ReservationDto's, each of them, in turn, contains back reference to its ClientDto owner.

You may check out this question for some ideas on how to deal with circular references with Automapper.

Community
  • 1
  • 1
Sergey Kolodiy
  • 5,829
  • 1
  • 36
  • 58