Automapper具有实体框架核心递归问题

问题描述 投票:0回答:1

[我与efcore 2.x有很多关系

然后我创建了3个类:

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;}
}

在我的dbcontext中,我建立了关系。一切正常。

但是当我想将Dto Place映射到我的对象位置中的Place对象时,我有一个反应性和溢出异常:

我有:

Place

|-> Users

      |-> User

      |-> Place

        |-> Users

          |-> ...

我在mapper的配置中尝试使用深度,但是它不起作用。

我发现的唯一工作空间是:

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

但这是一个丑陋的解决方案,一点都不方便。

那么我可以使用哪种解决方案?

谢谢,

我添加了自动映射器配置:

configuration.CreateMap<Place, PlaceDto>().MaxDepth(1); 
c# entity-framework-core automapper
1个回答
0
投票

仅不要映射该属性:

CreateMap<Foo, Bar>().ForMember(x => x.Blarg, opt => opt.Ignore());

© www.soinside.com 2019 - 2024. All rights reserved.