使用Mapster时,我希望在映射子对象时能够访问父对象。我该怎么做?
class Parent {
public int Id {get;set;}
public Child Child {get;set;}
}
class Child {
public int ParentId {get;set;}
public string Name {get;set;}
}
...
config.NewConfig<Parent, ParentModel>()
.Map(dest => dest.Id, src => src.Id)
.Map(dest => dest.Child, src => src.Child);
config.NewConfig<Child, ChildModel>()
.Map(dest => dest.Name, src => src.Name)
.Map(dest => dest.ParentId, src => // How do I access the parent.Id);
您可以在父映射配置中映射子对象。
config.NewConfig<Parent, ParentModel>()
.Map(dest => dest.Child.ParentId, src => src.Id);