[英]Automapper with Entity Framework Core recursive issue
I have a many-to-many relationship with EF Core 2.x.我与 EF Core 2.x 之间存在多对多关系。
I have created 3 classes:我创建了 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;}
}
In my dbcontext
, I set up the relationship.在我的
dbcontext
,我建立了关系。 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:但是当我想将我的 Dto Place 映射到我的对象位置中的我的地方对象时,我有一个递归和溢出异常:
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);
https://stackoverflow.com/a/48824922/8101300 https://stackoverflow.com/a/48824922/8101300
https://stackoverflow.com/a/57134333/8101300 https://stackoverflow.com/a/57134333/8101300
CreateMap<Foo, Bar>().PreserveReferences();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.