2015-12-22 33 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 
} 

我想將source/childsource類映射到dto類並忽略GrandChildSources屬性。忽略使用自動映射器的二級兒童

我已經嘗試使用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(); }); 

獲得錯誤 「缺失型地圖配置或不支持的映射GrandChildSource」

PS:LazyLoadingEnabled設置爲True。我得到堆棧溢出異常後,我決定忽略GrandChildSources屬性,因爲它有循環引用。

+0

你在做單向或雙向映射嗎? –

+0

如果我理解正確,我認爲它是單向映射 – Jack

回答

1

除非我失去了一些東西,這應該是相當簡單直白與映射:

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

或者你可以忽略GrandChildSourceDto的ChildSource財產避免你的循環引用的問題。

如果還有比這更復雜的事情,請說明問題所在。