如何使用扁平化dest映射嵌套源

时间:2019-08-07 09:57:46

标签: automapper

我正在尝试自动映射不是那么复杂的对象,但仍在努力地简化它。我正在使用自动映射器版本7

我试图用扁平对象映射嵌套源,但是我的目标类属性始终未映射。

Mapper.Map<IList<SrcParent>, IList<DestClass>>

所以我添加了另一个Dest父类

Mapper.Map<IList<SrcParent>, IList<Dest>>

映射除propertyD之外的所有所有属性,该属性始终为null。

public class SrcParent //Source parent class
{
  public Attributeclass Attribute {get ; set;}
  public AnotherAttrclass AnotherAttr {get ; set;}
}
public class Attributeclass // source child class1
{
   public propertyA  {get ; set;}
   public propertyB  {get ; set;}
    public propertyC  {get ; set;}
}
public class AnotherAttrclass // source child class 2
{
   public propertyD  {get ; set;}
}
public class Dest // destination parent class
{
   public DestClass DestClass {get ; set;}
}
public class DestClass // dest class with all the properties
{
   public propertyA {get ; set;}
   public propertyB {get ; set;}
   public propertyC {get ; set;}
   public propertyD  {get ; set;}
}

///我的映射个人资料如下所示

CreateMap<SrcParent,DestClass>()
.ForMember(d => d.propertyD, o => o.MapsFrom(s=>s.AnotherAttrClass));
CreateMap<AnotherAttrclass,DestClass>()
.ForMember(d => d.propertyD, o => o.MapsFrom(s => s.propertyD));

0 个答案:

没有答案