title: Adding item to collection navigation property in EF Core
body: <p>I have a <code>Project</code> class that contains a collection of <code>AppUsers</code></p>
  
  <pre><code>public class Project
  {
      public int ProjectID { get; set; }
  
      [Required(ErrorMessage = " Please enter a project name")]
      public string Name { get; set; }
  
      [Required(ErrorMessage = " Please enter a project description")]
      public string Description { get; set; }
  
      public virtual ICollection&lt;AppUser&gt; ProjectManagers { get; set; }
  
      public bool UserIsAlreadyPM(string userId)
      {
          foreach(AppUser user in this.ProjectManagers)
          {
              if(user.Id == userId)
              {
                  return true;
              }
          }
          return false;
      }
  }
  </code></pre>
  
  <p>My <code>AppUser</code> class</p>
  
  <pre><code>public class AppUser : IdentityUser
  {
      //add properties here later
  }
  </code></pre>
  
  <p>I want to be able to have the capability for any particular <code>AppUser</code> to be within the <code>ProjectManagers</code> of more than one <code>Project</code>. I add the <code>AppUser</code> to any <code>Project.ProjectManagers</code> via my repository method:</p>
  
  <pre><code>public void AddProjectManager(int projectID, AppUser user)
  {
        Project proj = context.Projects.FirstOrDefault(p =&gt; p.ProjectID == projectID);
        if(proj != null)
        {
            proj.ProjectManagers.Add(user);
            context.SaveChanges();
        }
  }
  </code></pre>
  
  <p>This works the first time the <code>AppUser</code> is added to a <code>Project.ProjectManagers</code> collection. However, this will not work if I attempt to add them to any other <code>Project.ProjectManagers</code> collection. If they are assign to any subsequent <code>Project.ProjectManagers</code> I get an primary key error because they are already in the database under a different project.</p>
  
  <p>"SqlException: Violation of PRIMARY KEY constraint 'PK_AppUser'. Cannot insert duplicate key in object 'dbo.AppUser'. The duplicate key value is (xxxx)."</p>