UseDestinationValue only when destination property is not null

后端 未结 3 1643
不知归路
不知归路 2021-01-18 08:59

How to configure AutoMapper mapping when I want to use behaviour from UseDestinationValue method, but only when destination property is NOT null.

3条回答
  •  Happy的楠姐
    2021-01-18 10:01

    I think the NullSubstitute option would work for you. See: http://weblogs.asp.net/psteele/archive/2011/03/18/automapper-handling-null-members.aspx

    EDIT

    Looks like you might need to add a little conditional logic to your mapping for Details (and skip the UseDestinationValue option):

    .ForMember(d => d.Details, 
        o => o.MapFrom(s => s.Details == null ? new ItemDetails() : Mapper.Map(s.Details))
    

提交回复
热议问题