AutoMapper - NullSubstitution

Sometimes you may want to provide default values if there are null values for a member in the source. Automapper has got a NullSubstitute method in configuration to specify a value for a memeber having null value.


NullSubstitute("<provide default value for null>"


var config = new MapperConfiguration(cfg =>

                    cfg.CreateMap<ModelData, ViewModel>()
						.ForMember(dest => dest.Name, opt => opt.NullSubstitute("Default Name"));


In this example, I have provided null for the Name property. But since I have used the NullSubstitute method in the configuration for that member, the value provided there will be mapped to destination object

No Comments

Add a Comment