title: Automapper with composite DTO (many-to-many)
body: <p>Not having more than a month's worth experience with EF and Automapper, I'm really battling to get the following done:</p>
  
  <p>I've got 3 DB entities as follow: (unnecessary fields remove for simplicity)
  <img src="https://i.stack.imgur.com/bHygu.jpg" alt="3 Entities as per DB Design"></p>
  
  <p>Within my project i have these entities pulled in through EF, and I created 2 DTO classes as follow:</p>
  
  <pre><code>[DataContract]
  public class StoreDTO
  {
      [DataMember]
      public int StoreID {get; set;}
  
      [DataMember]
      public string StoreName{get; set;}
  
      [DataMember]
      public List&lt;CategoryDTO&gt; Categories {get; set;}
  }
  
  [DataContract]
  public class CategoryDTO
  {
      [DataMember]
      public int CategoryID {get; set;}
  
      [DataMember]
      public string CategoryName{get; set;}
  
      [DataMember]
      public List&lt;StoreDTO&gt; Stores{get; set;}
  }
  </code></pre>
  
  <p>I'm using automapper to try and map the two classes but I'm really battling to get the composite relationship going. </p>
  
  <p>My map creation looks something like this now (after numerous tries):</p>
  
  <pre><code>Mapper.CreateMap&lt;Store, StoreDTO&gt;(); // Not performing the category list mapping yet here
  
  // This is where I attemped the mapping for a store list in each category:
  // x.Store_StoreCategory = StoreCategory as composite entity above
  Mapper.CreateMap&lt;Category, CategoryDTO&gt;()
      .ForMember(dto =&gt; dto.Stores,
                 opt =&gt; opt.MapFrom(x =&gt; Mapper.Map&lt;List&lt;Store&gt;, List&lt;StoreDTO&gt;&gt;(x.Store_StoreCategory.Select(y =&gt; y.Store).ToList())));
  </code></pre>
  
  <p>This isn't working though. I read a few articles and the latest i came across is this one by Jimmy Bogard himself:
  <a href="http://lostechies.com/jimmybogard/2014/03/12/avoid-many-to-many-mappings-in-orms/" rel="nofollow noreferrer">Avoid many-to-many mappings in ORMs</a></p>
  
  <p>He suggests creating the composite DTO as well and work from there which seems like a good way to go but I need help with the mapping as I'm a TSQL guy, not yet a LINQ guy with automapper and DTO capabilities... </p>
  
  <p>Other articles I tried were the following but with my lack of knowledge on Automapper and EF, I fear I might be missing the point somewhere as these articles don't refer to the composite DTO as suggested by Jimmy Bogard:</p>
  
  <p><a href="https://stackoverflow.com/questions/11782948/automapper-many-to-many-relationship-into-collection">AutoMapper many to many relationship into collection</a></p>
  
  <p><a href="https://github.com/AutoMapper/AutoMapper/wiki/Custom-value-resolvers" rel="nofollow noreferrer">https://github.com/AutoMapper/AutoMapper/wiki/Custom-value-resolvers</a></p>
  
  <p><a href="https://stackoverflow.com/questions/14441321/many-to-many-to-dtos-using-automapper">Many-to-many to DTOs using Automapper</a></p>
  
  <p>Would really appreciate some insight!</p>