映射到嵌套对象时找不到找不到的片段。
我有这样的目标对象:
public class OfferTimeHorizon: IEntityTimeHorzion
{
public int Id { get; set; }
public int OfferId { get; set; }
public LessonOffer Offer { get; set; }
public int TimeHorizonId { get; set; }
public TimeHorizon TimeHorizon { get; set; }
}
这样的源对象:
public class TimeHorizonVM
{
public int? Id { get; set; }
public DateTime? DatetimeFrom { get; set; }
public DateTime? DatetimeTo { get; set; }
}
自动映射器配置如下:
CreateMap<OfferTimeHorizon, TimeHorizonVM>()
.AfterMap((src, dest) => Mapper.Map(src.TimeHorizon, dest))
.ReverseMap()
.ForMember(dest => dest.Id, opt => opt.Ignore())
.ForMember(dest => dest.TimeHorizonId, opt => opt.MapFrom(src => src.Id))
.AfterMap((src, dest ) => Mapper.Map(src, dest.TimeHorizon));
CreateMap<TimeHorizon, TimeHorizonVM>().ReverseMap();
对于正向=> OfferTimeHorizon => TimeHorizonVM,对象已正确映射。
但对于相反的方向=> TimeHorizonVM => OfferTimeHorizon,仅映射TimeHorizonId。 TimeHorzion属性始终为null。
我在这里想念什么?