AutoMapper ForMember Ignore not working

孤者浪人 提交于 2019-12-07 03:34:01

问题


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). But setting the Id column to ignore in the map below is not working and the Id is being overwritten.

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:

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

});

回答1:


Your issue is that you're not giving automapper the existing object. Automapper can absolutely do this.

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. The above code will take the existing object and update values, as you expected.




回答2:


UPDATE:

The problem is when you assign 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.

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.

In your code, you are creating a new object with Id, CreatedById and CreatedOn properties with its default value.

You can use Mapper.Map(source, destination) overload that does exactly what you want:

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.



来源:https://stackoverflow.com/questions/35784612/automapper-formember-ignore-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!