且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

自动映射器UseDestinationValue

更新时间:2022-05-29 23:14:31

从不使用UseDestinationValue()选项,但是看起来您只是想从VPerson到DPerson时不映射ID.如果是这种情况,请使用忽略"选项:

Never used the UseDestinationValue() option, but it looks like you just want to NOT map the Id when going from VPerson to DPerson. If that is the case, use the Ignore option:

.ForMember(d => d.Id, o => o.Ignore());

编辑

哦,开枪-我什至没有注意到您使用的语法.您需要使用接受现有目标对象的地图"重载:

Oh shoot -- I didn't even notice the syntax you were using. You need to use the overload of "Map" that accepts the existing destination object:

Mapper.Map(vPerson, dPerson);

您使用的版本会创建一个新的DPerson,然后执行映射.我在上面显示的代码使用了已经创建的dPerson,然后执行了映射(使用上面显示的忽略"选项,您的ID不会被覆盖).

The version you're using creates a new DPerson and then performs the mappings. The one I show above takes the already-created dPerson and then performs the mappings (and with the Ignore option shown above, your Id is not overwritten).