title: Entity Framework Core: Adding a new entry in a 'join' table deletes existing records
body: <p>Some code removed for brevity. </p>
  
  <p>I created an ASP.NET Core MVC website using Entity Framework Core.  </p>
  
  <p>I have three tables for <code>Parts</code>, <code>Distributors</code> and <code>DistributorParts</code> (join table).  On my site, I have a page to add <code>Distributors</code> to a <code>Part</code>.  When I create a new <code>DistributorPart</code> record for <strong>Distributor 1</strong> and <strong>Part 1</strong>, the record is inserted correctly. But when I add a second <code>DistributorPart</code> record for <strong>Distributor 2</strong> and <strong>Part 1</strong>, the first <code>DistributorPart</code> record is deleted!  (note that the two aforementioned <code>DistributorPart</code> records are both using <strong>Part 1</strong>)</p>
  
  <p>My questions are: </p>
  
  <ul>
  <li>How can I get new <code>DistributorPart</code> records to be inserted <strong>without</strong> deleting/affecting the existing records?  </li>
  <li>What am I doing wrong? </li>
  </ul>
  
  <p>Models:</p>
  
  <pre><code>public class Part
  {
      [key]
      public int Id { get; set; }
  
      public string Description { get; set; }
  
      public ICollection&lt;DistributorPart&gt; DistributorParts { get; set; }
  }
  
  public class Distributor
  {
      [key]
      public int Id { get; set; }
  
      [Required]
      public string Name { get; set; }
  
      public ICollection&lt;DistributorPart&gt; DistributorParts { get; set; }
  }
  
  public class DistributorPart
  {
      [key]
      public int Id { get; set; }
  
      public int OnHand { get; set; }
  
      public int OnOrder { get; set; }
  
      public int PartId { get; set; }
  
      public Part Part { get; set; }
  
      public int DistributorId { get; set; }
  
      public Distributor Distributor { get; set; }
  }
  </code></pre>
  
  <p>Here is my <code>DbContext</code>:</p>
  
  <pre><code>public class MyContext : DbContext
  {
      public DbSet&lt;Part&gt; Parts { get; set; }
  
      public DbSet&lt;Distributor&gt; Distributors { get; set; }
  
      public DbSet&lt;DistributorPart&gt; DistributorParts { get; set; }
  
      public MyContext(DbContextOptions&lt;MyContext&gt; options) 
          : base(options) 
      { }
  }
  </code></pre>
  
  <p>Here is the code the inserts the new <code>DistributorPart</code> record:</p>
  
  <pre><code>public DistributorPart AddDistributorPart(DistributorPart distributorPart)
  {
      var newDistributorPart = new DistributorPart
      {
          OnHand = distributorPart.OnHand,
          OnOrder = distributorPart.OnOrder,
          PartId = distributorPart.PartId,
          DistributorId = distributorPart.DistributorId
      };
  
      _context.Add(newDistributorPart);
      _context.SaveChanges();
  
      return newDistributorPart;
  }
  </code></pre>
  
  <p>I can verify this in the Application Output window when debugging: </p>
  
  <blockquote>
    <p>Microsoft.EntityFrameworkCore.Database.Command:Information: Executed
    DbCommand  (1ms) [Parameters=[@p0='?', @p1='?', @p2='?'],
    CommandType='Text',  CommandTimeout='30'] DELETE FROM
    "DistributorParts" WHERE "Id" = @p0; ...</p>
  </blockquote>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Here's my Controller method that calls the AddDistributorPart() method:</p>
  
  <pre><code>public DistributorPart AddDistributorToPart([FromBody] DistributorPart distributorPart)
  {
      var part = _partService.GetPart(distributorPart.PartId);
      if (part == null || part.Id &lt;= 0)
      {
          throw new Exception($"Part Id of {distributorPart.PartId} does not exist.");
      }
  
      var distributor = _distributorService.GetDistributor(distributorPart.DistributorId);
      if (distributor == null || distributor.Id &lt;= 0)
      {
          throw new Exception($"Distributor Id of {distributorPart.DistributorId} does not exist.");
      }
  
      var userId = GetUserId();
      var newDistributorPart = _distributorPartService.AddDistributorPart(distributorPart, userId);
  
      ...
  
      return newDistributorPart;
  }
  </code></pre>