Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto Mapper Null Reference

Tags:

automapper

I have an issue with auto mapper which throws a Null reference exception.

Mapper.CreateMap<People, PeopleDto>()
      .ForMember(d => d.Country, opt => opt.MapFrom(o => o.Address.Country)) 

The problem is when Address is null and trying to get map Address.Country

like image 739
James Petersen Avatar asked Oct 31 '25 21:10

James Petersen


1 Answers

Mapper.CreateMap() 
    .ForMember(d => d.Country, 
        opt => opt.MapFrom(
            o => (o.Address != null) ? o.Address.Country : "ADDRESS NOT SPECIFIED"
        )
    )
like image 148
Peter K. Avatar answered Nov 02 '25 16:11

Peter K.