public void AddCoTheLevel(CoTheGameLevel gameLevel) { try { var coTheLevel = new CoTheLevel { LimitedMove = gameLevel.LimitedMoves }; caroContext.CoTheLevels.Add(coTheLevel); caroContext.SaveChanges(); foreach (var move in gameLevel.Moves) { var coTheMove = new CoTheMove { CoTheLevelId = coTheLevel.Id, Point = move }; caroContext.CoTheMoves.Add(coTheMove); } caroContext.SaveChanges(); } catch (Exception ex) { throw; } }
public void SaveCoTheLevel(CoTheGameLevel gameLevel) { try { var coTheLevels = caroContext.CoTheLevels.Include(level => level.CoTheMoves); if (coTheLevels != null) { var levelToModify = coTheLevels.SingleOrDefault(level => level.Id == gameLevel.Id); levelToModify.LimitedMove = gameLevel.LimitedMoves; if (levelToModify != null) { var coTheMoves = caroContext.CoTheMoves.ToList(); var coTheMovesToModify = levelToModify.CoTheMoves.Select(move => move.Point).ToList(); // find differences between two lists var movesToDelete = coTheMovesToModify.Except(gameLevel.Moves); foreach (var move in movesToDelete) { var moveToDelete = coTheMoves.SingleOrDefault(m => m.CoTheLevelId == levelToModify.Id && m.Point == move); caroContext.CoTheMoves.Remove(moveToDelete); } var movesToAdd = gameLevel.Moves.Except(coTheMovesToModify); foreach (var move in movesToAdd) { var moveToAdd = new CoTheMove { CoTheLevelId = levelToModify.Id, Point = move }; caroContext.CoTheMoves.Add(moveToAdd); } caroContext.SaveChanges(); } } } catch (Exception ex) { throw; } }