更新 m-to-m 关系,重复主键

Update m-to-m relationship, duplicate primary key

我正在尝试更新与列表框的多对多关系。我的实体模型如下所示:

我有一个带有复选框的列表框,用户可以在其中决定哪个球员在联赛中,哪个球员不在联赛中 (IsSelected-属性)。有两个问题:起初我无法选中然后取消选中播放器(它不会被删除)。第二个问题:第一次尝试,一切正常,当我再次进行选择时,出现以下异常:

  • _innerException {"An error occurred while updating the entries. See the inner exception for details."} System.Exception {System.Data.Entity.Core.UpdateException}
  • _innerException {"Violation of PRIMARY KEY constraint 'PLID'. Cannot insert duplicate key in object 'dbo.PlayerLeague'. The duplicate key value is (2, 2).\r\nThe statement has been terminated."} System.Exception {System.Data.SqlClient.SqlException}
using (BettingLeagueEntities entities = new BettingLeagueEntities())
{
    foreach (PlayerCheckBoxList p in this.PlayerList)
    {
        if(p.IsSelected == true)
        {
            PlayerLeague pl = new PlayerLeague();
            pl.League = this.ActiveLeague;
            pl.Player = p.ActivePlayer;

            var local = entities.Set<Player>().Local.FirstOrDefault(x => x.PID == p.ActivePlayer.PID);
            if(local != null)
            {
                entities.Entry(local).State = System.Data.Entity.EntityState.Detached;
            }

            var localLeague = entities.Set<League>().Local.FirstOrDefault(x => x.LID == this.ActiveLeague.LID);
            if (localLeague != null)
            {
                entities.Entry(localLeague).State = System.Data.Entity.EntityState.Detached;
            }

            if (entities.Entry(p.ActivePlayer).State == System.Data.Entity.EntityState.Detached)
            {
                entities.Player.Add(p.ActivePlayer);
                entities.Entry(p.ActivePlayer).State = System.Data.Entity.EntityState.Modified;
            }
            if (entities.Entry(this.ActiveLeague).State == System.Data.Entity.EntityState.Detached)
            {
                entities.League.Add(this.ActiveLeague);
                entities.Entry(this.ActiveLeague).State = System.Data.Entity.EntityState.Modified;
            }

            if(p.ActivePlayer.PlayerLeague.All(x => x.LID != this.ActiveLeague.LID))
            {
                p.ActivePlayer.PlayerLeague.Add(pl);
                this.ActiveLeague.PlayerLeague.Add(pl);
            }
        }
        else
        {
            PlayerLeague local = entities.Set<PlayerLeague>().Local.FirstOrDefault(x => x.LID == this.ActiveLeague.LID && x.PID == p.ActivePlayer.PID);
            if(local != null)
            {
                entities.PlayerLeague.Attach(local);
                entities.PlayerLeague.Remove(local);
            }

            entities.SaveChanges();
        }
    }
    entities.SaveChanges();
}

我不知道如何解决这个问题,你有什么建议吗?

我有!我试着稍微评论一下,让它易于理解。

第一个问题是我最近检查了我的 PlayerLeague 是否已经存在。我在第一个 if(statement).

中移动了这个条件

第二个错误是,在我的 else 块中,我查找 playerleague 的语句总是返回 null。现在我检查是否有任何实体,如果这是真的,我将其删除。

using (BettingLeagueEntities entities = new BettingLeagueEntities())
{
    foreach (PlayerCheckBoxList p in this.PlayerList)
    {
        // Check if the Player is seleceted and if the ActivePlayer has the Active League
        if (p.IsSelected == true && p.ActivePlayer.PlayerLeague.All(x => x.LID != this.ActiveLeague.LID))
        {
            // Define the new PlayerLeague
            PlayerLeague pl = new PlayerLeague {PID = p.ActivePlayer.PID, LID = this.ActiveLeague.LID};

            var localPlayer = entities.Set<Player>().Local.FirstOrDefault(x => x.PID == p.ActivePlayer.PID);

            if (localPlayer != null)
            {
                entities.Entry(localPlayer).State = System.Data.Entity.EntityState.Detached;
            }

            if (entities.Entry(p.ActivePlayer).State == System.Data.Entity.EntityState.Detached)
            {
                entities.Player.Add(p.ActivePlayer);
                entities.Entry(p.ActivePlayer).State = System.Data.Entity.EntityState.Modified;
            }

            var localLeague = entities.Set<League>().Local.FirstOrDefault(x => x.LID == this.ActiveLeague.LID);

            if (localLeague != null)
            {
                entities.Entry(localLeague).State = System.Data.Entity.EntityState.Detached;
            }

            if (entities.Entry(this.ActiveLeague).State == System.Data.Entity.EntityState.Detached)
            {
                entities.League.Add(this.ActiveLeague);
                entities.Entry(this.ActiveLeague).State = System.Data.Entity.EntityState.Modified;
            }

            p.ActivePlayer.PlayerLeague.Add(pl);
            this.ActiveLeague.PlayerLeague.Add(pl);
        }
        else
        {
            // Check if there is a PlayerLeague for this Player and league
            bool hasPlayerLeague =
                entities.PlayerLeague.Any(x => x.LID == this.ActiveLeague.LID && x.PID == p.ActivePlayer.PID);

            if (hasPlayerLeague && p.IsSelected == false)
            {
                // Find PlayerLeague
                PlayerLeague pl =
                        entities.PlayerLeague.FirstOrDefault(
                        x => x.LID == this.ActiveLeague.LID && x.PID == p.ActivePlayer.PID);

                // Attach and Remove PlayerLeague
                entities.PlayerLeague.Attach(pl);
                entities.PlayerLeague.Remove(pl);
            }

            entities.SaveChanges();
        }
    }
}