0

When using Mapster I'd like to have access to the parent object when mapping the children. How do I do this?

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);
Fosol
  • 137
  • 1
  • 6

1 Answers1

0

You can map child object inside parent mapping configuration.

config.NewConfig<Parent, ParentModel>()
    .Map(dest => dest.Child.ParentId, src => src.Id);
  • 3
    Although this code might solve the problem, a good answer should also explain **what** the code does and **how** it helps. – BDL Apr 19 '20 at 08:56