2012-11-29 39 views
1

考慮:DTO現有的對象沒有失去現有對象數據的其餘

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()); 
+0

謝謝先生。但是有沒有比這更簡單的方法?如果我的原始對象有太多的其他屬性沒有包含在DTO對象中,那將是痛苦的。 ValueInjecter怎麼樣? – ghazyy