9

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?

Community
  • 1
  • 1
khorvat
  • 1,630
  • 2
  • 20
  • 31

2 Answers2

11

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.

andre
  • 371
  • 1
  • 4
  • 8
8

Change the mapping to use ForMember:

map.ForMember(s => s.DateCreated, opt => opt.Ignore());
map.ForMember(s => s.DateUpdated, opt => opt.Ignore());
Gruff Bunny
  • 27,738
  • 10
  • 72
  • 59
  • 2
    Yes that did the trick, but for me it's a bit strange to set the ignore on destination member when doing the mapping because I'm actually ignoring the data in the source not the destination. – khorvat Nov 29 '13 at 09:11
  • 3
    I made the same mistake too. I blame AutoMapper's documentation. It's not written for beginners that's for sure. Somehow after reading the wiki and googling, I couldn't get a definitive answer. Until now. – iphone007 May 14 '14 at 19:18
  • This should not be marked as the answer: SourceDto.Foo DestiationDto //Foo not a member!!! – Brian Ogden Jul 06 '23 at 05:15