简体   繁体   English

AutoMapper ForMember忽略不起作用

[英]AutoMapper ForMember Ignore not working

Doing a copy of the same entity type in an MVC app, but looking to ignore copying the primary key (doing an update to an existing entity). 在MVC应用中执行相同实体类型的副本,但希望忽略复制主键(对现有实体进行更新)。 But setting the Id column to ignore in the map below is not working and the Id is being overwritten. 但是将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())
    ; 

Executing the Map: 执行地图:

existingStratusVendorContact = Mapper.Map<VendorContact>(vendorContact);

Saw this other answer , but it appears I am doing that already. 看到了另一个答案 ,但看来我已经在这样做了。

UPDATE: 更新:

Fyi, I am creating my maps in the Global.asax like so: 仅供参考,我正在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())
        ;  

});

Your issue is that you're not giving automapper the existing object. 您的问题是您没有给automapper现有的对象。 Automapper can absolutely do this. Automapper绝对可以做到这一点。

Mapper.Map<VendorContact>(vendorContact, existingStratusVendorContact);

Should do what you want. 应该做你想要的。 You current code is creating a brand new object, and replacing existingStratusVendorContact with the entirely new object. 您当前的代码正在创建一个全新的对象,并用全新的对象替换existingStratusVendorContact的StratusVendorContact。 The above code will take the existing object and update values, as you expected. 上面的代码将采用现有的对象并按预期方式更新值。

UPDATE: 更新:

The problem is when you assign Mapper.Map<VendorContact>(vendorContact); 问题是当您分配Mapper.Map<VendorContact>(vendorContact); to existingStratusVendorContact you are replacing the current value of the variable with the returned by Map() method, no matter what properties you are ignoring. existingStratusVendorContact ,无论您忽略什么属性,都将用Map()方法返回的值替换变量的当前值。

With Mapper.Map(source) you can project an object to a new object of other type copying properties according some conventions, but you are creating a new object. 使用Mapper.Map(source)可以根据一些约定将对象投影到其他类型的复制属性的新对象,但是您正在创建一个新对象。

In your code, you are creating a new object with Id , CreatedById and CreatedOn properties with its default value. 在代码中,您将使用IdCreatedByIdCreatedOn属性及其默认值创建一个新对象。

You can use Mapper.Map(source, destination) overload that does exactly what you want: 您可以使用Mapper.Map(source, destination)重载来实现您想要的功能:

Mapper.Map<VendorContact>(vendorContact, existingStratusVendorContact);

ORIGINAL: 原版的:

If you are creating your maps like this: 如果要像这样创建地图:

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());
});

You need to create a mapper with this configuration: 您需要使用以下配置创建一个映射器:

var mapper = cfg.CreateMapper();

And use it to map the objects: 并使用它来映射对象:

var existingStratusVendorContact = mapper.Map<VendorContact>(vendorContact);

If you use the static class Mapper the default behavior is used and properties are mapped. 如果使用静态类Mapper ,则会使用默认行为并映射属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM