自动映射:对现有实体对象进行更新

问题描述 投票:0回答:1

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

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

任何的想法?

asp.net-mvc entity-framework-6 automapper
1个回答
0
投票

如果仍然出现问题,请尝试以下方法:

public void UpdateEntity(Customer customerSrc)
{
    MYPOCO pocoDesc= dbContext.DD_POCO.SingleOrDefault(m => m.Id == 123);
    AutoMapper.Mapper.Map<Customer, MYPOCO>(customerSrc, pocoDesc);
    dbContext.Save();
}
© www.soinside.com 2019 - 2024. All rights reserved.