我使用自动Map器将一个类属性与两个不同的其他类进行Map,如下所示:
var emp = map.Map<EmployeeAddressDto, Employee>(employeeAddressDto);
上面的Map为我提供了如下所示的对象:
Address1 = "some xyz address1"
Address2 = "some xyz address2"
Phone = "0000000000"
然后用EmployeeOtherAddressDto
Map输出对象
map.Map<EmployeeOtherAddressDto, Employee>(emp);
上面的Map将my out更改为:
Address1 = "some other xyz address1"
Address2 = "some other xyz address2"
Phone = null
但我希望它像下面这样:
Address1 = "some other xyz address1"
Address2 = "some other xyz address2"
Phone = "0000000000"
当上面的Map发生时,它使phone不为空,但我希望它保持在以前的Map中定义的状态,尽管我希望覆盖Address1
和Address2
字段。
以下是我的Map:
CreateMap<EmployeeAddressDto, Employee>()
.ForMember(dest => dest.Address1, options => options.MapFrom(src => src.Address1))
.ForMember(dest => dest.Address2, options => options.MapFrom(src => src.Address2))
.ForMember(dest => dest.Phone, options => options.MapFrom(src => src.Phone))
.ValidateMemberList(MemberList.None)
.ForAllMembers(opts => opts.Condition((src, dest, srcMember) => srcMember != null));
CreateMap<EmployeeOtherAddressDto, Employee>()
.ForMember(dest => dest.Address1, options => options.MapFrom(src => src.Address1))
.ForMember(dest => dest.Address2, options => options.MapFrom(src => src.Address2))
.ValidateMemberList(MemberList.None)
.ForAllMembers(opts => opts.Condition((src, dest, srcMember) => srcMember != null));
我想这样做,在我的Map配置文件,因为我正在使用它在许多地方,不想更新手机没有使用Map器。
1条答案
按热度按时间3vpjnl9f1#
请尝试使用此方法配置Map。
Map后〈〉()
你可以在Map后检查你的字段是否为空。