0

在此处输入图像描述
在我的场景中,我与单个表有多对多的关系。所以在 Player_VsMode 我有两个 Player 表的外键。一个是给玩家的,第二个是给对手的。当我试图在表 Player_VsMode 中插入时。在 Player_vsMode 中插入数据一切正常,但在 Player 中它添加了一行新的对手 Object。这对我来说没有意义,为什么地狱实体框架会添加这一新的对手行!我的代码是

 public Void performVsMode(Player_VsMode vsMode)
    {
        Player playerFromDB = legendWarsEntities.Player.First(v => v.email == vsMode.Player.email);
        Player enemyFromDB = legendWarsEntities.Player.First(v => v.email == vsMode.Opponent.email);
        vsMode.PlayerReference.Value = playerFromDB;
        vsMode.OpponentReference.Value = enemyFromDB;
        legendWarsEntities.AddToPlayer_VsMode(vsMode);
        legendWarsEntities.SaveChanges();

    }
4

1 回答 1

0

我不推荐这个 只是做了一个工作来告诉项目经理它正在工作:)。

  Player playerFromDB = legendWarsEntities.Player.First(v => v.email == vsMode.Player.email);
        Player enemyFromDB = legendWarsEntities.Player.First(v => v.email == vsMode.Opponent.email);
        vsMode.PlayerReference.Value = playerFromDB;
        vsMode.OpponentReference.Value = enemyFromDB;
        //adding records
        legendWarsEntities.SaveChanges();
        //deleting extra Opponent Object!!!
        Player[] playerToDel = legendWarsEntities.Player.Where(v => v.email == vsMode.Opponent.email).ToArray();
        legendWarsEntities.DeleteObject(playerToDel[1]);
        legendWarsEntities.SaveChanges();
于 2012-04-12T10:30:54.253 回答