0

Suppose I have the following classes

public class Parent {
    public int ParentId { get; set; }
    public List<Child> Children{get; set; }
    // other properties
}

public class Child {
    public int ChildId { get; set; }
    // other properties
}


public class ParentVm {
    public int ParentId { get; set; }
    public List<ChildVm> Children{get; set; }
    // other properties
}

public class ChildVm {
    public int ParentId { get; set; }
    public int ChildId { get; set; }
    // other properties
}

Parent parent = /*something*/
var parentVm = Mapper.Map<ParentVm>(parent);

In the ChildVm, there is property ParentId. How can I propagate that value to using automapper?

1
  • It doesn't appear by your question that you are trying to map ChildVm.ParentId to anything. Also, it appears that your Mapper.Map command is backward if you are trying to create a ParentVm, as that line will create a Parent object. Is the real use case trying to map a Parent object, which has Child objects to a ParentVm which has ChildVm objects? And to have the Parent.ParentId property propagate to the ChildVm.ParentId property? Commented Oct 5, 2017 at 16:59

1 Answer 1

3

I'm making some assumptions with your question.

  1. You are trying to map Parent to ParentVm (and Child to ChildVm).
  2. You are trying to map Parent.ParentId to ChildVm.ParentId.

If both of these are true, then the following code snippet (from LINQPad) should help you. Note the use of AfterMap in the first CreateMap.

void Main()
{
    var config = new MapperConfiguration(cfg =>
    {
        cfg.CreateMap<Parent, ParentVm>().AfterMap((s, d) =>
        {
            foreach (var child in d.Children)
            {
                child.ParentId = d.ParentId;
            }
        });
        cfg.CreateMap<Child, ChildVm>();
    });

    var mapper = config.CreateMapper();

    var parent = new Parent { ParentId = 1, Children = new List<Child>() };
    var child1 = new Child { ChildId = 1 };
    var child2 = new Child { ChildId = 2 };
    parent.Children.Add(child1);
    parent.Children.Add(child2);

    var parentVm = mapper.Map<Parent, ParentVm>(parent);
    parentVm.Dump();
}

public class Parent
{
    public int ParentId { get; set; }
    public List<Child> Children { get; set; }
    // other properties
}

public class Child
{
    public int ChildId { get; set; }
    // other properties
}


public class ParentVm
{
    public int ParentId { get; set; }
    public List<ChildVm> Children { get; set; }
    // other properties
}

public class ChildVm
{
    public int ParentId { get; set; }
    public int ChildId { get; set; }
    // other properties
}

Output:

LINQPad Dump Results

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.