AutoMapper's Ignore() not working when using ForSourceMember?

左心房为你撑大大i 提交于 2019-12-19 18:48:13

问题


I'm trying to ignore a property from source type. I have defined mapping like this:

var map = AutoMapper.Mapper.CreateMap<Article, IArticle>();
map.ForSourceMember(s => s.DateCreated, opt => opt.Ignore());
map.ForSourceMember(s => s.DateUpdated, opt => opt.Ignore());

When I call Map function,

AutoMapper.Mapper.Map(article, articlePoco);

destination's properties gets updated anyway. I'm using the latest stable version downloaded from NuGet.

Any ideas why this isn't working ?

I have found similar question to this one but there is no answer attached. [question]:AutoMapper's Ignore() not working?


回答1:


Change the mapping to use ForMember:

map.ForMember(s => s.DateCreated, opt => opt.Ignore());
map.ForMember(s => s.DateUpdated, opt => opt.Ignore());



回答2:


If the property that you want to ignore only exists in the source object then you can you MemberList.Source in combination with the option method DoNotValidate(). See below:

CreateMap<IArticle, Article>(MemberList.Source)
    map.ForSourceMember(src => src.DateCreated, opt=> opt.DoNotValidate());
    map.ForSourceMember(src => src.DateUpdated, opt => opt.DoNotValidate());

This is perfect if you are using AssertConfigurationIsValid and want to ignore validation of certain source properties.



来源:https://stackoverflow.com/questions/20267098/automappers-ignore-not-working-when-using-forsourcemember

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