AutoMapper ForMember 忽略不工作
AutoMapper ForMember Ignore not working
在 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);
看到了this other answer,但看来我已经在做了。
更新:
仅供参考,我正在 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())
;
});
更新:
问题是,当您将 Mapper.Map<VendorContact>(vendorContact);
赋值给 existingStratusVendorContact
时,无论您忽略了哪些属性,您都会用 Map()
方法返回的值替换变量的当前值。
使用 Mapper.Map(source)
您可以根据一些约定将一个对象投影到其他类型的复制属性的新对象,但您正在创建一个新对象。
在您的代码中,您正在创建一个具有 Id
、CreatedById
和 CreatedOn
属性及其默认值的新对象。
您可以使用 Mapper.Map(source, destination)
重载来完全满足您的需求:
Mapper.Map<VendorContact>(vendorContact, existingStratusVendorContact);
原文:
如果您是这样创建地图的:
var cfg = new MapperConfiguration(c =>
{
c.CreateMap<VendorContact, VendorContact>()
.ForMember(dest => dest.Id, option => option.Ignore())
.ForMember(dest => dest.CreatedById, option => option.Ignore())
.ForMember(dest => dest.CreatedOn, option => option.Ignore());
});
您需要使用此配置创建映射器:
var mapper = cfg.CreateMapper();
并用它来映射对象:
var existingStratusVendorContact = mapper.Map<VendorContact>(vendorContact);
如果您使用静态 class Mapper
,则使用默认行为并映射属性。
您的问题是您没有为自动映射器提供现有对象。 Automapper 绝对可以做到这一点。
Mapper.Map<VendorContact>(vendorContact, existingStratusVendorContact);
应该做你想做的。您当前的代码正在创建一个全新的对象,并将 existingStratusVendorContact
替换为全新的对象。如您所料,上面的代码将获取现有对象并更新值。
在 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);
看到了this other answer,但看来我已经在做了。
更新:
仅供参考,我正在 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())
;
});
更新:
问题是,当您将 Mapper.Map<VendorContact>(vendorContact);
赋值给 existingStratusVendorContact
时,无论您忽略了哪些属性,您都会用 Map()
方法返回的值替换变量的当前值。
使用 Mapper.Map(source)
您可以根据一些约定将一个对象投影到其他类型的复制属性的新对象,但您正在创建一个新对象。
在您的代码中,您正在创建一个具有 Id
、CreatedById
和 CreatedOn
属性及其默认值的新对象。
您可以使用 Mapper.Map(source, destination)
重载来完全满足您的需求:
Mapper.Map<VendorContact>(vendorContact, existingStratusVendorContact);
原文:
如果您是这样创建地图的:
var cfg = new MapperConfiguration(c =>
{
c.CreateMap<VendorContact, VendorContact>()
.ForMember(dest => dest.Id, option => option.Ignore())
.ForMember(dest => dest.CreatedById, option => option.Ignore())
.ForMember(dest => dest.CreatedOn, option => option.Ignore());
});
您需要使用此配置创建映射器:
var mapper = cfg.CreateMapper();
并用它来映射对象:
var existingStratusVendorContact = mapper.Map<VendorContact>(vendorContact);
如果您使用静态 class Mapper
,则使用默认行为并映射属性。
您的问题是您没有为自动映射器提供现有对象。 Automapper 绝对可以做到这一点。
Mapper.Map<VendorContact>(vendorContact, existingStratusVendorContact);
应该做你想做的。您当前的代码正在创建一个全新的对象,并将 existingStratusVendorContact
替换为全新的对象。如您所料,上面的代码将获取现有对象并更新值。