更新不适用于多对多关系。 Entity Framework核心2.2

Update is not working for many-to-many relationship. Entity Framework Core 2.2

我在游戏和类别之间有很多对多的关系。我正在使用 Sql 服务器。一个游戏可以有多个类别。创建操作有效,但更新无效。出现错误:

InnerException: Violation of PRIMARY KEY constraint 'PK_GameCategories'. Cannot insert duplicate key in object 'dbo.GameCategories'. The duplicate key value is (1003, 1).\r\nThe statement has been terminated.

public class Game
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Display(Name = "Game Name")]
    public string Name { get; set; }

    [Display(Name = "Game Categories")]
    public ICollection<GameCategory> GameCategories { get; set; }
}

public class Category
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Display(Name = "Game Category")]
    public string Name { get; set; }

    [Display(Name = "Game Categories")]
    public ICollection<GameCategory> GameCategories { get; set; }
}

public class GameCategory
{
    public int GameId { get; set; }
    public Game Game { get; set; }
    public int CategoryId { get; set; }
    public Category Category { get; set; }
}

// --------------------------
GameController/Edit Action [Working Perfectly]
// -------------------------
[HttpGet]
[Route("edit-game/{id}")]
public async Task<IActionResult> EditGame(int id)
{
      try
      {
                if (id == null) return NotFound();
                var gameInDb = await _db.Games.FirstOrDefaultAsync(g => g.Id == id);
                if (gameInDb == null) return NotFound();

                var model = new GameChangeViewModel()
                {
                    Title = $"Edit {gameInDb.Name}",
                    Name = gameInDb.Name,
                    // Displaying list of categories
                    GameCategoryList = _db.Categories
                        .Select(c => new SelectListItem() { Text = c.Name, Value = c.Id.ToString()}).ToList(),
                    // loading selected values
                    GameCategoryId = _db.GameCategories.Where(c=>c.GameId == gameInDb.Id).Select(s=>s.CategoryId).ToList()
                };

                return View(model);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return NotFound();
            }
        }

下面的代码不工作并引发上述异常。

[HttpPost]
        [Route("edit-game-post")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> EditGamePost(GameChangeViewModel model)
        {
            try
            {
                if (!ModelState.IsValid) return View(nameof(EditGame), model);

                var gameInDb = await _db.Games.FirstOrDefaultAsync(g => g.Id == model.Id);
                if (gameInDb == null)
                {
                    ModelState.AddModelError("", "No Data");
                    return View(nameof(EditGame), model);
                }

                _db.GameCategories.RemoveRange(gameInDb.GameCategories);
                _db.SaveChanges();

                gameInDb.Name = model.Name;
                var category = _db.Categories.Where(c => model.GameCategoryId.Contains(c.Id)).ToList();
                foreach (Category cat in category)
                {
                    gameInDb.GameCategories.Add(new GameCategory()
                    {
                        Category = cat, Game = gameInDb
                    });
                }

                await _db.SaveChangesAsync();
                return RedirectToAction(nameof(Index));

            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                ModelState.AddModelError("", "Exception Happen");
                return View(nameof(EditGame), model);
            }
        }


遗憾的是,我还没有找到任何适合多对多关系的增删改查操作。谢谢

尝试在 GameCategory table 中创建新的列 ID 作为主键。