title: The INSERT statement conflicted with the FOREIGN KEY constraint in EF Core
body: <p>There are numerous questions in varying contexts regarding this error and so far I've been unable to find one that applies to my situation.</p>
  
  <p>I have a many to many relationship I'm trying to establish with a 3rd table. It's basically a person to manager association. All managers are people but not all people are managers and also, every person has many managers and every manager has many people. So in the manager's table are PersonIds. In the people controller under edit is the option to associate people with managers. When clicking the available managers and clicking save, I get the error:</p>
  
  <blockquote>
    <p>The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.ManagerToEngineer_dbo.Manager_ManagerId". The conflict occurred in database "PROJECTNAME_16ce3f6a2a6c4ff0b1ce147d126984ba", table "dbo.Manager", column 'ManagerId'. The statement has been terminated.</p>
  </blockquote>
  
  <p>The SaveChanges method results in this error:</p>
  
  <pre><code>        [HttpPost]
          [ValidateAntiForgeryToken]
          public ActionResult Edit(PersonViewModel person)
          {
              if (ModelState.IsValid)
              {
                  //var MyPerson = db.people.Find(person.PersonId);
                  //MyPerson.FirstName = person.FirstName;
                  //MyPerson.LastName = person.LastName;
                  foreach (var item in db.ManagersToEngineers)
                  {
                      if (item.EngineerId == person.PersonId)
                      {
                          db.Entry(item).State = EntityState.Deleted;
                      }
                  }
  
                  foreach (var item in person.EngineerManagers)
                  {
                      if (item.Checked)
                      {
                          db.ManagersToEngineers.Add(new ManagerToEngineer() { EngineerId = person.PersonId, ManagerId = item.Id });
                      }
                  }
                  //db.Entry(person).State = EntityState.Modified;
                  db.SaveChanges();
                  return RedirectToAction("Index");
              }
              return View(person);
          }
  </code></pre>
  
  <p>Relevant models:</p>
  
  <pre><code>public class ManagerToEngineer
      {
          [Key]
          public int ManagerToEngineerId { get; set; }
  
          [Required]
          [ForeignKey("engineer")]
          public int EngineerId { get; set; }
  
          [Required]
          [ForeignKey("manager")]
          public int ManagerId { get; set; }
  
          public virtual Person engineer { get; set; }
          public virtual Manager manager { get; set; }
  
      }
  
  public class Manager
  {
      [Key]
      public int ManagerId { get; set; }
  
      [Required]
      [ForeignKey("person")]
      public int EngineerId { get; set; }
  
      public virtual Person person { get; set; }
      public ICollection&lt;ManagerToEngineer&gt; ManagersToEngineers { get; set; }
  }
  
  public class PersonViewModel
  {
      public int PersonId { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
  
      [Display(Name = "Managers")]
      public List&lt;CheckBoxViewModel&gt; EngineerManagers { get; set; }
  }
  
  public class CheckBoxViewModel
  {
      public int Id { get; set; }
      public string FullName { get; set; }
      public bool Checked { get; set; }
  }
  </code></pre>