Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# automapper nested collections

Tags:

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

like image 793
Raffaeu Avatar asked Nov 24 '09 13:11

Raffaeu


People also ask

What is C in coding language?

C is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, and so on. C programming is an excellent language to learn to program for beginners. Our C tutorials will guide you to learn C programming one step at a time.

What is C language w3schools?

C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Who made C?

C, computer programming language developed in the early 1970s by American computer scientist Dennis M. Ritchie at Bell Laboratories (formerly AT&T Bell Laboratories).


1 Answers

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(); 
like image 56
Raffaeu Avatar answered Sep 17 '22 19:09

Raffaeu