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
}
我想将源/子源类映射到 dto 类并忽略 GrandChildSources 属性。
我曾尝试使用 UseDestinationValue 和 Ignore,但它似乎不起作用。
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。我决定在得到 Stack overflow 异常后忽略 GrandChildSources 属性,因为它有循环引用。