我遇到了自动映射器的问题,它会抛出Null引用异常。
Mapper.CreateMap<People, PeopleDto>()
.ForMember(d => d.Country, opt => opt.MapFrom(o => o.Address.Country))
问题是当Address为null并尝试获取地图Address.Country
时答案 0 :(得分:4)
Mapper.CreateMap() .ForMember(d => d.Country, opt => opt.MapFrom(o => (o.Address != null) ? o.Address.Country : "ADDRESS NOT SPECIFIED"))