自动映射器空引用

时间:2011-06-15 15:00:59

标签: automapper

我遇到了自动映射器的问题,它会抛出Null引用异常。

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

问题是当Address为null并尝试获取地图Address.Country

1 个答案:

答案 0 :(得分:4)

Mapper.CreateMap() .ForMember(d => d.Country, opt => opt.MapFrom(o => (o.Address != null) ? o.Address.Country : "ADDRESS NOT SPECIFIED"))