0

I have a many-to-many relationship with EF Core 2.x.

I have created 3 classes:

public class Place
{
   public string Name {get; set;}
   public int Id {get; set;}
   public ICollection<PlaceUser> Users {get; set;}
}

public class User
{
   public string Name {get; set;}
   public int Id {get; set;}
   public ICollection<PlaceUser> Places {get; set;}
}

public class PlaceUser
{
   public int UserId{get; set;}
   public User User{get; set;}
   public int PlaceId{get; set;}
   public Place Place{get; set;}
}

public class PlaceDto
{
   public string Name {get; set;}
   public int Id {get; set;}
   public ICollection<PlaceUserDto> Users {get; set;}
}

In my dbcontext, I set up the relationship. Everything works well.

But when I want to Map my Dto Place to my Place Object in my object place I have a recursivity and overflow exception:

I have:

Place

|-> Users

      |-> User

      |-> Place

        |-> Users

          |-> ...

I tried in my config of mapper to use depth but it's not working.

The only workaround I have found is:

if(place!=null && place.Users!=null)
{  // I set all the place.Users[i].Place = null; and place.Users[i].User=null;}

But it's an ugly solution and not convenient at all.

So which solution can I use?

Thanks,

I added the automapper config:

configuration.CreateMap<Place, PlaceDto>().MaxDepth(1); 
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
dalton5
  • 915
  • 2
  • 14
  • 28
  • Please show the code how you use automapper. ProjectTo? That does not support recursion at all - sadly. Hidden in the docs, i know. – TomTom Mar 07 '20 at 02:04
  • of course you have recursion. Placeuser is inside place and place is in placeuser. I'm not an expert but try putting Virtual in front of both of them like in all examples. Virtual only loads data when needed. – John Lord Mar 07 '20 at 02:21
  • Please [edit] your question to include the source code you have as a [mcve], which can be compiled and tested by others. – Progman Mar 07 '20 at 09:30
  • The main use case for `ProjectTo` is EF6. So, generally speaking, `ProjectTo` supports what EF6 supports. EF Core is a whole different beast. – Lucian Bargaoanu Mar 07 '20 at 10:29
  • I added the automapper config I use – dalton5 Mar 07 '20 at 16:28
  • Looks like `PlaceUserDto` has a backreference to `PlaceDto`. If so, remove it. – Gert Arnold Mar 07 '20 at 18:37

1 Answers1

0

https://stackoverflow.com/a/48824922/8101300

https://stackoverflow.com/a/57134333/8101300

CreateMap<Foo, Bar>().PreserveReferences();

jasonvuriker
  • 255
  • 1
  • 12
  • I need the value for PlaceUser once. Then for sublevels I don't need anymore. In your proposition it will set null at the first level placeuser. – dalton5 Mar 07 '20 at 19:49
  • @dalton5 Changed my answer. Try to use it – jasonvuriker Mar 07 '20 at 19:56
  • Nothing works. I tried all. In many to many efcore entities I have recurence and loading of all the entities recursively. And I can't change them.. – dalton5 Mar 23 '20 at 16:26