22

I have a simple model like this one:

public class Order{
   public int Id { get; set; }
   ... ...
   public IList<OrderLine> OrderLines { get; set; }
}

public class OrderLine{
   public int Id { get; set; }
   public Order ParentOrder { get; set; }
   ... ...
}

What I do with Automapper is this:

    Mapper.CreateMap<Order, OrderDto>();
    Mapper.CreateMap<OrderLine, OrderLineDto>();
    Mapper.AssertConfigurationIsValid();

It throw an exception that says: "The property OrderLineDtos in OrderDto is not mapped, add custom mapping ..." As we use a custom syntax in our Domain and in our DomainDto, how I can specify that the collection OrderLineDtos in OrderDto corresponds to OrderLines in Order?

Thank you

3
  • 1
    Have you tried putting the OrderLine CreateMap ahead of the Order mapping? Commented Nov 24, 2009 at 13:03
  • 1
    Can you post what your Dto's look like? And an example of the custom syntax you use? We would need that to provide you with an example of a custom mapping. Commented Nov 24, 2009 at 13:12
  • 1
    If I exchange the order I receive a different error ... The OrderDto in OrderLineDto is not mapped ... Do you know how to use custom mapping expression? Commented Nov 24, 2009 at 13:12

2 Answers 2

22

It works in this way:

    Mapper.CreateMap<Order, OrderDto>()
        .ForMember(dest => dest.OrderLineDtos, opt => opt.MapFrom(src => src.OrderLines));
    Mapper.CreateMap<OrderLine, OrderLineDto>()
        .ForMember(dest => dest.ParentOrderDto, opt => opt.MapFrom(src => src.ParentOrder));
    Mapper.AssertConfigurationIsValid();
Sign up to request clarification or add additional context in comments.

1 Comment

Did you know that you can edit your original post, regardless of your reputation? It's always good to keep any information updates in the original question.
8

Nested collections work, as long as the names match up. In your DTOs, you have the name of your collection as "OrderLineDtos", but in the Order object, it's just "OrderLines". If you remove the "Dtos" part of the OrderLineDtos and ParentOrderDto property names, it should all match up.

3 Comments

So, does that mean mapping nested collections of different class types is not working currently? Sometimes the nested object is a ViewModel and has properties that need to be mapped differently. thanks!
Will it use the same concrete ICollection/IList type as defined in TDestination?
@Jimmy Bogard: There are many times that the nested object itself is a DTO or ViewModel.

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.