自动映射,如何使字段不映射

时间:2017-08-07 09:44:14

标签: automapper

源类有2个属性,目标类3属性,映射,我认为在目标类中有1个属性不变,我使用NotMapped,但没有成功,但NotMapped不是我想要的方式,你是什么办?

class AAA
{
    public string Name { set; get; }
    public string Id { set; get; }
    public string Remark { set; get; }
}

class AAAViewModel
{
    public string Name { set; get; }
    public string Id { set; get; }
}

protected override MapperConfiguration Configuration => new MapperConfiguration(cfg=>
{          
    cfg.CreateMap<AAA, AAAViewModel>(MemberList.Destination);
    cfg.CreateMap<AAAViewModel, AAA>(MemberList.Source);
});

[Fact]
public void test()
{
    AAA a = new AAA() { Id = "1", Name = "name1", Remark = "remark1" };
    var avm = Mapper.Map<AAAViewModel>(a);
    AAA b = new AAA() { Remark = "remakrb" };
    b = Mapper.Map<AAA>(avm);
    Assert.Equal(avm.Id, "1");
}

1 个答案:

答案 0 :(得分:0)

该属性称为IgnoreMap。