title: EF Core Many To Many Duplicate entries
body: <p>I'm trying to setup a many to many relationship using EF Core and ASP.NET Core 2.1 and as for now I cannot get this working and it seems I do not understand the logic behind it either.</p>
  
  <p>So I have setup many to many relationship using ModelBuilder like this:</p>
  
  <pre><code>builder.Entity&lt;ComponentWare&gt;().HasKey(cw =&gt; new { cw.ComponentId, cw.WareId });
              builder.Entity&lt;ComponentWare&gt;()
                  .HasOne(x =&gt; x.Component)
                  .WithMany(x =&gt; x.ComponentWares)
                  .HasForeignKey(x =&gt; x.ComponentId);
              builder.Entity&lt;ComponentWare&gt;()
                  .HasOne(x =&gt; x.Ware)
                  .WithMany(x =&gt; x.ComponentWares)
                  .HasForeignKey(x =&gt; x.WareId);
  
  
              base.OnModelCreating(builder);
  </code></pre>
  
  <p>My entity models:</p>
  
  <pre><code>public class Component
  {
      public int ComponentId { get; set; }
      public string Number { get; set; }
      public string Name { get; set; }
      public string MaterialType { get; set; }
      public decimal? Cost { get; set; }
      public float? Weight { get; set; }
      public sbyte ComponentType { get; set; }
      public sbyte SourceType { get; set; }
      public string Comment { get; set; }
      public string Author { get; set; }
      public string AddedBy { get; set; }
      public DateTime? ModifiedDate { get; set; }
  
      public virtual ICollection&lt;BookComponent&gt; BookComponents { get; set; }
  
      public virtual ICollection&lt;ComponentWare&gt; ComponentWares { get; set; } 
  }
  
  public class Ware
  {
      public int WareId { get; set; }
      public string Code { get; set; }
      public string Name { get; set; }
      public decimal? Quantity { get; set; }
      public string Unit { get; set; }
      public decimal? Converter { get; set; }
      public DateTime? Date { get; set; }
  
      public virtual ICollection&lt;ComponentWare&gt; ComponentWares { get; set; }
  }
  </code></pre>
  
  <p>And my join table:</p>
  
  <pre><code>public class ComponentWare
  {
      public int ComponentId { get; set; }
      public Component Component { get; set; }
  
      public int WareId { get; set; }
      public Ware Ware { get; set; }
  
      public int Quantity { get; set; }
      public float Length { get; set; }
      public string Unit { get; set; }
  
  }
  </code></pre>
  
  <p>Now, I want to setup a many to many relationship between Ware and Component tables so that in the end my join table would look like this.</p>
  
  <pre><code>Tables:   WareId    |   ComponentId    |        ComponentWares         |
              1       |      1           | ComponentId  |      WareId    |
              2       |      2           |      1       |        1       |
                                         |      1       |        1       |
                                         |      2       |        1       |
                                         |      2       |        1       |
  </code></pre>
  
  <p>So many components could have many wares ( also duplicated ) and vice versa.</p>
  
  <p>I have tried to manually add entries using SQL Server Explorer but it seems I cannot add multiple ComponentId and WareId with the same values because of the</p>
  
  <pre><code>HasKey(cw =&gt; new { cw.ComponentId, cw.WareId })
  </code></pre>
  
  <p>I've read that the line above is necessary for many to many relationship in EF Core but for my understanding it denies the idea of many to many relationship...</p>
  
  <p>Should I remove <strong>ComponentId cw.WareId</strong> key from the ModelBuilder and add Id in the join table or is the another solution for this ?</p>