在MVC应用程序中做同样的实体类型的副本,但是希望忽略复制主键(对现有实体进行更新)。但在下面的地图中将Id列设置为忽略不起作用,并且Id被覆盖。AutoMapper ForMember忽略不工作
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);
锯this other answer,但现在看来,我这样做了。
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())
;
});
'existingStratusVendorContact'是一个现有的对象,你想替换除'Id','CreatedById','CreatedOn'之外的属性吗?或者用这个属性的默认值创建一个新的? –
是的,前者。更新除这3个属性以外的现有对象。 –