Automapper映射,如何使字段未映射

Automapper映射,如何使字段未映射

问题描述:

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

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"); 
} 

该属性被称为IgnoreMap。