所以我有這個;AutoMapper - 嵌套映射,同時保留選定的子屬性
public class Parent
{
public string SomeProperty { get; set; }
public Child ChildProperty { get; set; }
}
public class Child
{
public string ChildProperty { get; set; }
public string OtherChildProperty { get; set; }
}
public class Flat
{
public string SomeProperty { get; set; }
public string ChildProperty { get; set; }
}
然後我這樣做;
Flat source = new Flat { SomeProperty = "test", ChildProperty = "test" };
Parent dest = GetParentFromDataContext();
Mapper.Map<Flat,Parent>(source,dest);
然後我的期望是,dest.ChildProperty.OtherChildProperty
仍設置爲不管它是什麼,當它被從DataContext的拉動。不過,我正在努力做到這一點。
如果我CreateMap
這樣,那麼我得到一個「必須解析爲頂級成員」的異常;
Mapper.CreateMap<Flat,Parent>()
.ForMember(dest => dest.Parent.ChildProperty.ChildProperty,
options => options.MapFrom(source => source.ChildProperty))
.ForMember(dest => dest.Parent.ChildProperty.OtherChildProperty,
options => options.Ignore());
但是,如果我這樣做,那麼new Child {}
替換從本質上DataContext的結算OtherChildProperty
拉Child
;
Mapper.CreateMap<Flat,Parent>()
.ForMember(dest => dest.Child
options => options.MapFrom(source => new Child { ChildProperty = source.ChildProperty }));
如何映射這個並保留我希望忽略的子屬性?