【问题标题】:Automapper: To do an update on existing entity objectAutomapper:对现有实体对象进行更新
【发布时间】:2018-05-25 15:29:31
【问题描述】:

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

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】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多