Without having seen your mapping code it is hard to say exactly what is going wrong but my guess is that you are mapping your types with code similar to the following:
Mapper.CreateMap<OrderItem, OrderItemDTO>()
.ForMember(dest => dest.VersionId, options => options.MapFrom(orderitem => orderitem.Version.VersionId))
.ForMember(dest => dest.VersionName, options => options.MapFrom(orderitem => orderitem.Version.VersionName))
;
The code above will fail when OrderItem.Version
is null. To prevent this you can check for null in the delegates passed to ForMember
:
Mapper.CreateMap<OrderItem, OrderItemDTO>()
.ForMember(dest => dest.VersionId, options => options.MapFrom(orderitem => orderitem.Version == null ? (int?) null : orderitem.Version.VersionId))
.ForMember(dest => dest.VersionName, options => options.MapFrom(orderitem => orderitem.Version == null ? null : orderitem.Version.VersionName))
;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…