自动映射器相同的对象类型映射(将字段从一个对象复制到另一个)

时间:2018-12-18 14:42:52

标签: c# automapper

我有以下内容:

    public async Task<bool> Update(CoinEntity coinEntity)
    {

        var found = await context.Coins.FindAsync(coinEntity.CoinId);
        if(found==null)
        {
            throw new CoinNotFoundException(coinEntity.CoinId);
        }

        Mapper.Initialize(cfg => cfg.CreateMap<CoinEntity, CoinEntity>());
        found = Mapper.Map<CoinEntity>(coinEntity);
        await context.SaveChangesAsync();

        return true;


    }

从某种意义上说,找到的对象具有传入的所有属性,但是我认为,因为.Map创建了一个新实例。 EFCore不再对此进行跟踪,并且SaveChanges保持不变。

自动映射器有办法真正更新实际对象而不是创建新实例吗?

我只是希望在自动映射器完成其映射后,EF仍可以跟踪我的found对象。

我试图避免必须逐个字段复制字段,而使用EF则不是一种选择,因为它不起作用,因为我需要替换子表记录(删除一些记录,添加一些记录)

0 个答案:

没有答案