[英]Automapper with Entity Framework Core recursive issue
我與 EF Core 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
|-> Users
|-> User
|-> Place
|-> Users
|-> ...
我嘗試在我的映射器配置中使用深度,但它不起作用。
我發現的唯一解決方法是:
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);
https://stackoverflow.com/a/48824922/8101300
https://stackoverflow.com/a/57134333/8101300
CreateMap<Foo, Bar>().PreserveReferences();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.