many to many relation ships

76 views
Skip to first unread message

fkar...@amplisine.com

unread,
Dec 12, 2014, 11:32:53 AM12/12/14
to automapp...@googlegroups.com
I found out to resolve one to many, many to one and one to one relationships however I am still unclear about how to resolve many to many relationships while mapping.

this sample and the solution of one<->many relationships from following link: http://stackoverflow.com/questions/11505128/circular-reference-causing-stack-overflow-with-automapper

public class OrderDTO
{
    // Standard properties
    public virtual int OrderId { get; set; }
    public virtual DateTime OrderDate { get; set; }
    public virtual string Address { get; set; }

    // Child collection properties
    public virtual IList<OrderLineDTO> OrderLines { get; set; } 
}

public class OrderLineDTO
{
    // Standard properties
    public virtual int OrderLineId { get; set; }
    public virtual string Description { get; set; }
    public virtual decimal Amount { get; set; }

    public virtual OrderDTO Order { get; set; } 
}
and the solution

Mapper.Map<OrderLine, OrderLineDTO>()
    .ForMember(m => m.Order, opt => opt.Ignore());

Mapper.Map<Order, OrderDTO>()
    .AfterMap((src, dest) => { 
         foreach(var i in dest.OrderLines) 
             i.Order = dest;
         });


What if it would be something like this;

OrderDTO.cs public virtual IList<OrderLineDTO> OrderLineDTOs { get; set; }

OrderLineDTO.cs public virtual IList<OrderDTO> OrderDTOs { get; set; }


any idea would be appreciated.

















Jimmy Bogard

unread,
Dec 12, 2014, 5:41:19 PM12/12/14
to automapp...@googlegroups.com
Why do your DTOs have references back up to the OrderDto?
--
You received this message because you are subscribed to the Google Groups "AutoMapper-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to automapper-use...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

fkar...@amplisine.com

unread,
Dec 22, 2014, 6:17:22 PM12/22/14
to automapp...@googlegroups.com
So, in ideal case I shouldn't have DTOs which are referencing each other (one to one or many to many)? is that what you are saying?
In fact, what I was trying to is decoupling the entity classes from my viewmodels, and models since they include unnecessary properties, so I created same classes as entity classes but without additional EF related properties by using T4 script. My DTOs are more like POCO classes. So, they have references to each other many-to-many (which means two separate classes include each other's reference as List) or one-to-many.

Do you have suggestion for decoupling the entity classes from my viewmodels, and models?
Is my path wrong from the beginning if not how can I have automapper to eliminate circular may-to many references therefore having stack overflow exception?
...

Jimmy Bogard

unread,
Dec 24, 2014, 8:12:43 PM12/24/14
to automapper-users
I guess I was just curious why you had those relationships - do you have code using those bidirectional DTO paths?

--
Reply all
Reply to author
Forward
0 new messages