title: Many-to-many relationship creating duplicates
body: <p>I'm creating a many-to-many relationship which involves three tables, one table for the Game object and one table for the Player object. </p>
  
  <p>Every time I create a new game, which should reference existing players (unless new players are involved), new duplicate players of existing players are created... I created a function to try and solve this issue... it looks like this. I think I'm close to solving the problem, but I have no idea what to put into the else code block to get the game to map to the existing players.</p>
  
  <pre><code>private void SaveNewPlayers(Common.Game newGame, Data.Game dataGame)
  {
      foreach (var player in newGame.Players)
      {
          var isNewPlayer = player.PlayerId &lt; 1;  //The reason I do this is because on the front-end, 
  //when a new player is written in rather than selected they receive a negative ID so they can be distinguished
          var playerData = MapToData(player); //This just converts the front end Common.Player 
  //to a replica Data.Player object so it's database ready
  
          if (isNewPlayer)
          {
              Context.Players.Add(playerData);
              dataGame.Players.Add(playerData);
              Context.SaveChanges();
          }
          else
          {
  
  
              //Context.Players.Add(playerData);
              //Context.SaveChanges(); //Doing this adds clone Players to the database but not the relational database
  
              // dataGame.Players.Add(playerData); //Doing this adds clone Players and relationship to the database
              //Context.SaveChanges();
  
  
          }
      }
  
  }
  </code></pre>
  
  <p>I feel like a simple function I'm not aware of should address this, but I'm having a hard time finding what I need.  Apologies if this has been asked, I looked, but couldn't find something as specific as this.</p>
  
  <p>Thanks.</p>