Vishwakant
Vishwakant

Reputation: 111

AutoMapper Inline Ignore

I am using Auto Mapper to map source to destination object, I have configured my mapper like this:

Mapper.Initialize(cfg => {    
   cfg.CreateMap< SourceModel, DestModel>();   
}

This source and dest object mapping is being used in many places, now in some cases, I have to ignore one of source model field, but not for all places. I could do like this:

 CreateMap< SourceModel, DestModel>()
   .ForMember(x => x.CreatedDateTime, opt => opt.Ignore());

But this will ignore CreatedDateTime property for all scenario, so I want to do it inline only.

 Mapper.Map< DestModel>(sourceObject); //Here I want to ignore one property.

Please help me how I can achieve this.

Upvotes: 1

Views: 898

Answers (1)

majita
majita

Reputation: 1326

Sounds like you need conditional mapping.

This answer on SO shows how to use it and the documentation can be found here.

example usage:

Mapper.CreateMap<SourceModel, DestModel>()
 .ForMember(dest => dest.CreatedDateTime, opt => opt.Condition(source => source.Id == 0))

Upvotes: 1

Related Questions