简体   繁体   English

多对多关系创建重复项

[英]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. 我正在创建一个多对多关系,其中涉及三个表,一个表用于Game对象,一个表用于Player对象。

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. 我认为我已经快要解决问题了,但是我不知道要在else代码块中放入什么以使游戏映射到现有玩家。

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. 谢谢。

You may be forgetting to add the attached entity to dataGame : 您可能会忘记将附加的实体添加到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. 该实体必须来自数据库,而不是一些新实例。

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();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM