我有这种情况:
我从数据库中获得了一个实体
// Get the pending row where transactionId == paypal invoice
var receiptToComplete = (await _receiptService.SingleOrDefaultAsync(payPalCheckoutInfo.invoice, StatusConstant.PENDING));
然后我做这样的映射:
var receiptId = receiptToComplete.Id; // Should put the Id in a temp variable to reassign after mapping, otherwise will be 0
receiptToComplete = Mapper.Map<WebServiceCheckout, Receipt>(payPalCheckoutInfo);
receiptToComplete.Id = receiptId; // Reassign id
如何使用AutoMapper 覆盖 Id字段?
我试过
cfg.CreateMap<WebServiceCheckout, Receipt>()
.ForMember(m => m.Id, x => x.Ignore())
但它不起作用。我得到了值0。 我的AutoMapper版本是5.0.0
大卫
答案 0 :(得分:1)
Mapper.Map的重载不需要你完全覆盖你的目的地。
Mapper.Map<WebServiceCheckout, Receipt>(payPalCheckoutInfo, receiptToComplete);