我想从另一个模型更新现有的实体对象。但每次我得到一个新对象(具有映射属性和其他属性的默认值。)相反,我想要一个部分更新的目标对象。
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.
}
Automapper:6.2.2(版本)
试过Automapper: Update property values without creating a new object
任何的想法?
如果仍然出现问题,请尝试以下方法:
public void UpdateEntity(Customer customerSrc)
{
MYPOCO pocoDesc= dbContext.DD_POCO.SingleOrDefault(m => m.Id == 123);
AutoMapper.Mapper.Map<Customer, MYPOCO>(customerSrc, pocoDesc);
dbContext.Save();
}