【问题标题】:DTO to existing object without loosing rest of existing object data对现有对象进行 DTO,而不会丢失其余现有对象数据
【发布时间】:2012-11-29 09:01:39
【问题描述】:

考虑:

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 价值

有没有办法在保持原始值的同时进行映射?

【问题讨论】:

  • 你能展示你的创建地图配置吗?

标签: c# automapper dto


【解决方案1】:

您应该能够在创建地图配置中“忽略”目标值:

Mapper.CreateMap<DTOContact,OriginalContact>()
  .ForMember(dest => dest.CustomerCode, options => options.Ignore());

【讨论】:

  • 谢谢您,先生。但是还有比这更简单的方法吗?如果我的原始对象有太多其他属性未包含在 DTO 对象中,那将是痛苦的。 ValueInjecter 呢?
猜你喜欢
  • 1970-01-01
  • 2015-11-27
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 2021-10-08
  • 1970-01-01
  • 1970-01-01
  • 2014-04-02
相关资源
最近更新 更多