考虑一下:
class OriginalContact
{
public int Id{set;get;}
public string Name{set;get;}
public string CustomerCode{set;get;}
}
class DTOContact
{
public int Id{set;get;}
public string Name{set;get;}
}
OriginalContact originalContact = new OriginalContact
{
CustomerCode="123";
}
DTOContact dtoContact= new DTOContact
{
Id=1,
Name="David"
}
Mapper.Map(dtoContact, originalContact);
此映射后,我将失去 CustomerCode 值
有没有办法在保留原始值的同时进行映射?
答案 0 :(得分:1)
您应该能够“忽略”创建地图配置中的目标值:
Mapper.CreateMap<DTOContact,OriginalContact>()
.ForMember(dest => dest.CustomerCode, options => options.Ignore());