Nics1225
Nics1225

Reputation: 236

Many-to-many relationship creating duplicates

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.

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.

private void SaveNewPlayers(Common.Game newGame, Data.Game dataGame)
{
    foreach (var player in newGame.Players)
    {
        var isNewPlayer = player.PlayerId < 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();


        }
    }

}

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.

Thanks.

Upvotes: 0

Views: 297

Answers (2)

Nics1225
Nics1225

Reputation: 236

What ended up working for me:

private void SaveNewPlayers(Common.Game newGame, Data.Game dataGame)
{
    foreach (var player in newGame.Players)
    {
        var isNewPlayer = player.PlayerId < 1; 
        var playerData = MapToData(player);
        if (isNewPlayer)
        {
            Context.Players.Add(playerData);
            dataGame.Players.Add(playerData);
            Context.SaveChanges();
        }
        else
        {
            EditPlayer(player, dataGame);
        }
    }

}

and then created this function

    public void EditPlayer(Common.Player alteredPlayer, Data.Game thisGame)
    {
        var playerData = Context.Players.Where(p => p.PlayerId == alteredPlayer.PlayerId).First();

        playerData.PlayerId = alteredPlayer.PlayerId;
        playerData.Goals = alteredPlayer.Goals
        /*etc... etc...*/
        playerData.Games = playerData.Games;
        playerData.Games.Add(thisGame);

        Context.SaveChanges();
    }

Upvotes: 0

beautifulcoder
beautifulcoder

Reputation: 11340

You may be forgetting to add the attached entity to dataGame:

var pData = Context.Players.Add(playerData);
dataGame.Players.Add(pData);
Context.SaveChanges();

The entity has to come from the db not some newed up instance.

Upvotes: 1

Related Questions