2015-12-22 6 views
2
public class Source 
{ 
    public ChildSource ChildSource { get; set; } 
    //some other properties 
} 

public class ChildSource 
{ 
    public List<GrandChildSource> GrandChildSources { get; set; } 
    //some other properties 
} 

public class GrandChildSource 
{ 
    Public ChildSource ChildSource { get; set; } 
    //some other properties 
} 

And Dto classes: 

public class SourceDto 
{ 
    public ChildSourceDto ChildSource { get; set; } 
    //some other properties 
} 

public class ChildSourceDto 
{ 
    public List<GrandChildSourceDto> GrandChildSources { get; set; } 
    //some other properties 
} 

public class GrandChildSourceDto 
{ 
    Public ChildSourceDto ChildSource { get; set; } 
    //some other properties 
} 

Я хотел бы сопоставить классы источника/childsource классам dto и игнорировать свойство GrandChildSources.Игнорировать ребенка второго уровня с помощью automapper

Я попытался использовать UseDestinationValue и игнорировать, но, похоже, он не работает.

Mapper.CreateMap<Source, SourceDto>() 
       .ForMember(dest => dest.ChildSource, opt => { opt.UseDestinationValue(); opt.Ignore(); }) 
       .AfterMap((source, destination) => Mapper.Map(source.ChildSource, destination.ChildSource)); 

Mapper.CreateMap<ChildSource, ChildSourceDto>() 
       .ForMember(d => d.GrandChildSources, opt => { opt.UseDestinationValue(); opt.Ignore(); }); 

Получение ошибки "конфигурации Missing типа карты или неподдерживаемый отображение для GrandChildSource"

PS: LazyLoadingEnabled установлено значение True. Я решил проигнорировать свойство GrandChildSources после получения исключения переполнения стека, поскольку он получил круговую ссылку.

+0

Вы делаете одностороннее или двустороннее сопоставление? –

+0

Если я правильно понимаю, я думаю, что это одностороннее сопоставление – Jack

ответ

1

Если я что-то не хватает, то это должно быть довольно просто с прямыми сопоставлениями

Mapper.CreateMap<Source, SourceDto>(); 
Mapper.CreateMap<ChildSource, ChildSourceDto>() 
    .ForMember(dest => dest.GrandChildSources, opt => opt.Ignore()); 

В качестве альтернативы вы можете игнорировать ChildSource собственности на GrandChildSourceDto, чтобы избежать вашей проблему циклических ссылок.

Если у вас есть что-то более сложное, пожалуйста, уточните, в чем проблема.