在 MVC 应用程序中复制相同实体类型,但希望忽略复制主键(对现有实体进行更新)。但是在下面的地图中将 Id 列设置为忽略不起作用,并且 Id 被覆盖。
cfg.CreateMap<VendorContact, VendorContact>()
.ForMember(dest => dest.Id, option => option.Ignore())
.ForMember(dest => dest.CreatedById, option => option.Ignore())
.ForMember(dest => dest.CreatedOn, option => option.Ignore())
;
执行地图:
existingStratusVendorContact = Mapper.Map<VendorContact>(vendorContact);
Saw 这个另一个答案 https://stackoverflow.com/questions/20267098/automappers-ignore-not-working-when-using-forsourcemember,但看来我已经在这样做了。
UPDATE:
仅供参考,我正在 Global.asax 中创建地图,如下所示:
Mapper.Initialize(cfg =>
{
cfg.CreateMap<VendorContact, VendorContact>()
.ForMember(dest => dest.Id, option => option.Ignore())
.ForMember(dest => dest.CreatedById, option => option.Ignore())
.ForMember(dest => dest.CreatedOn, option => option.Ignore())
;
});