title: Automapper many to many mapping
body: <p>Patrick, thanks for advice about correct question!</p>
  
  <p><strong>EDIT 1:</strong></p>
  
  <p>I have three table for many to many relationship. Like this:
  <img src="https://i.stack.imgur.com/yfcEJ.jpg" alt="EF data model"></p>
  
  <p>GoodEntity:</p>
  
  <pre><code>public partial class GoodEntity
  {
      public GoodEntity()
      {
          this.GoodsAndProviders = new HashSet&lt;GoodAndProviderEntity&gt;();
      }
  
      public int id { get; set; }
      public string name { get; set; }
      public string description { get; set; }
      public decimal cost { get; set; }
      public Nullable&lt;decimal&gt; price { get; set; }
  
      public virtual ICollection&lt;GoodAndProviderEntity&gt; GoodsAndProviders { get; set; }
  }
  </code></pre>
  
  <p>ProviderEntity:</p>
  
  <pre><code>public partial class ProviderEntity
  {
      public ProviderEntity()
      {
          this.GoodsAndProviders = new HashSet&lt;GoodAndProviderEntity&gt;();
      }
  
      public int id { get; set; }
      public string name { get; set; }
      public string description { get; set; }
      public string address { get; set; }
      public string phone { get; set; }
      public string email { get; set; }
      public string url { get; set; }
      public Nullable&lt;int&gt; rating { get; set; }
  
      public virtual ICollection&lt;GoodAndProviderEntity&gt; GoodsAndProviders { get; set; }
  }
  </code></pre>
  
  <p>Entity for many-to-many relationship:</p>
  
  <pre><code>public partial class GoodAndProviderEntity
  {
      public int id { get; set; }
      public int good_id { get; set; }
      public int provider_id { get; set; }
  
      public virtual GoodEntity Goods { get; set; }
      public virtual ProviderEntity Providers { get; set; }
  }
  </code></pre>
  
  <p>GoodDTO:</p>
  
  <pre><code>public class GoodDTO
  {
      public int id { get; set; }
      public string name { get; set; }
      public string description { get; set; }
      public decimal cost { get; set; }
      public decimal? price { get; set; }
  
      public IList&lt;ProviderDTO&gt; providers { get; set; }
  }
  </code></pre>
  
  <p>ProviderDTO:</p>
  
  <pre><code>public class ProviderDTO
  {
      public int id { get; set; }
      public string name { get; set; }
      public string description { get; set; }
      public string address { get; set; }
      public string phone { get; set; }
      public string email { get; set; }
      public string url { get; set; }
      public int? rating { get; set; }
  }
  </code></pre>
  
  <p>This is code for creation maps:</p>
  
  <pre><code>Mapper.CreateMap&lt;ProviderDTO, ProviderEntity&gt;();
  Mapper.CreateMap&lt;ProviderEntity, ProviderDTO&gt;();
  
  Mapper.CreateMap&lt;GoodEntity, GoodDTO&gt;()
        .ForMember(dto =&gt; dto.providers, opt =&gt; opt.MapFrom(x =&gt; x.GoodsAndProviders));
  Mapper.CreateMap&lt;GoodAndProviderEntity, ProviderDTO&gt;();
  </code></pre>
  
  <p>And it works half. Automapper was mapped "goods" completely and was created list for all providers for this goods. But automapper don`t fill providers.
  <img src="https://i.stack.imgur.com/m8lMx.jpg" alt="enter image description here"></p>
  
  <p>If I use Mapper.AssertConfigurationIsValid(), then:</p>
  
  <blockquote>
    <p>Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type ======================================================= ProviderDTO -> ProviderEntity (Destination member list) Core.DTO.ProviderDTO -> DAL.EF.Entities.ProviderEntity (Destination member list) Unmapped properties: GoodsAndProviders ============================================================== GoodAndProviderEntity -> ProviderDTO (Destination member list) DAL.EF.Entities.GoodAndProviderEntity -> Core.DTO.ProviderDTO (Destination member list)</p>
  </blockquote>
  
  <p>How to create mapping for many-to-many relationship?</p>
  
  <p>Regards, Anton</p>