title: EF Core 5 - Updating an entity in many-to-many relation throws "Violation of PRIMARY KEY" error
body: <p>I am working with <a href="https://docs.microsoft.com/en-us/dotnet/core/dotnet-five" rel="nofollow noreferrer">.NET 5</a> and using <a href="https://docs.microsoft.com/en-us/ef/core/" rel="nofollow noreferrer">EF Core</a> and <code>MSSQL</code> for database.</p>
  <p>I have 2 classes that have many to many relationship between them. Those were migrated from models to DB.
  <a href="https://i.stack.imgur.com/rshfz.png" rel="nofollow noreferrer">example of DB tables</a></p>
  <p>Migration:</p>
  <pre><code>migrationBuilder.CreateTable(
              name: &quot;TournamentUser&quot;,
              columns: table =&gt; new
              {
                  TournamentsId = table.Column&lt;int&gt;(type: &quot;int&quot;, nullable: false),
                  UsersId = table.Column&lt;int&gt;(type: &quot;int&quot;, nullable: false)
              },
              constraints: table =&gt;
              {
                  table.PrimaryKey(&quot;PK_TournamentUser&quot;, x =&gt; new { x.TournamentsId, x.UsersId });
                  table.ForeignKey(
                      name: &quot;FK_TournamentUser_Tournaments_TournamentsId&quot;,
                      column: x =&gt; x.TournamentsId,
                      principalTable: &quot;Tournaments&quot;,
                      principalColumn: &quot;Id&quot;,
                      onDelete: ReferentialAction.Cascade);
                  table.ForeignKey(
                      name: &quot;FK_TournamentUser_Users_UsersId&quot;,
                      column: x =&gt; x.UsersId,
                      principalTable: &quot;Users&quot;,
                      principalColumn: &quot;Id&quot;,
                      onDelete: ReferentialAction.Cascade);
              });
  </code></pre>
  <p>Models:</p>
  <pre><code>public class Tournament
  {
      [Key]
      public int Id { get; set; }
      [Required, MaxLength(20)]
      public string Name { get; set; }
      [Required]
      public int MaxPlayers { get; set; }
      public int PlayersCount { get; set; }
      [Required]
      public DateTime Start { get; set; }
      public bool IsStarted { get; set; }
      public bool IsEnded { get; set; }
      public List&lt;User&gt; Users { get; set; }
  }
  
  public class User
  {
      [Key]
      public int Id { get; set; }
      [Required, MaxLength(15)]
      public string Username { get; set; }
      [Required]
      public string Password { get; set; }
      [Required, MaxLength(20)]
      public string Name { get; set; }
      [Required, MaxLength(20)]
      public string Surname { get; set; }
      [JsonIgnore]
      public List&lt;Tournament&gt; Tournaments { get; set; }
  }
  </code></pre>
  <p>So as I try to update Tournaments model</p>
  <pre class="lang-cs prettyprint-override"><code>[HttpPut]
  public async Task&lt;ActionResult&lt;Tournament&gt;&gt; Put([FromBody] Tournament tournament)
  {
      _context.Tournaments.Update(tournament);
      await _context.SaveChangesAsync();
  
      return CreatedAtAction(nameof(GetTournament), new { id = tournament.Id }, tournament);
  }
  </code></pre>
  <p>I get this error</p>
  <blockquote>
  <p>Microsoft.Data.SqlClient.SqlException (0x80131904): Violation of
  PRIMARY KEY constraint 'PK_TournamentUser'. Cannot insert duplicate
  key in object 'dbo.TournamentUser'. The duplicate key value is (1, 1).</p>
  </blockquote>
  <p>What could be possible solutions for this? I would like to keep my many to many table udpated as <code>Tournament's User</code> can be removed or added each time I update <code>Tournament</code>.</p>
  <p><strong>EDIT:</strong><br />
  Following shows the tournament object data -</p>
  <p><img src="https://i.stack.imgur.com/KbTyS.png" alt="Example of updatable tournament object" /></p>