Automapper:对现有实体对象进行更新

LMK*_*LMK 5 asp.net-mvc automapper entity-framework-6

我想从另一个模型更新现有实体对象。但每次我得到一个新对象(具有映射的属性和其他属性的默认值。)相反,我想要一个部分更新的目标对象。

AutoMapper.Mapper.Initialize(cfg => cfg.CreateMap<Customer, MYPOCO>().ReverseMap());


public void UpdateEntity(Customer customerSrc)
{
    MYPOCO pocoDesc= dbContext.DD_POCO.SingleOrDefault(m => m.Id == 123);
    pocoDesc = AutoMapper.Mapper.Map<Customer, MYPOCO>(customerSrc, pocoDesc);

  // Here "pocoDesc" is a new object, I got only "customerSrc" data and lost all other existing properties values.
}
Run Code Online (Sandbox Code Playgroud)

自动映射器:6.2.2(版本)

尝试自动映射器:更新属性值而不创建新对象

任何想法?

小智 5

如果问题仍然存在,请尝试以下操作:

public void UpdateEntity(Customer customerSrc)
{
    MYPOCO pocoDesc= dbContext.DD_POCO.SingleOrDefault(m => m.Id == 123);
    AutoMapper.Mapper.Map<Customer, MYPOCO>(customerSrc, pocoDesc);
    dbContext.DD_POCO.Update(pocoDesc);
    dbContext.Save();
}
Run Code Online (Sandbox Code Playgroud)