public async Task UpdateRound(RoundEditModel round) { if (round == null) { throw new ArgumentNullException(nameof(round)); } // Update own props var roundToUpdate = await _context.Round.SingleAsync(s => s.Id == round.Id); roundToUpdate.Season = round.Season; roundToUpdate.Venue = round.Venue; roundToUpdate.Note = round.Note; roundToUpdate.Date = round.Date; roundToUpdate.Number = round.Number; roundToUpdate.Name = round.Name; roundToUpdate.Rain = round.Rain.HasValue ? (int)round.Rain.Value : new int?(); roundToUpdate.Status = round.Status.ToString(); roundToUpdate.Rating = round.Rating; roundToUpdate.WinningTeam = round.WinningTeamId; _context.Update(roundToUpdate); await _context.SaveChangesAsync(); // Update winners using (var transactionalQueryExecutor = _queryExecutor.BeginTransaction()) { try { await transactionalQueryExecutor .NewQuery("DELETE FROM [dbo].[RoundWinner] WHERE [Round]=@Round") .WithCommandType(CommandType.Text) .WithParameters(new { Round = round.Id }) .ExecuteAsync(); var winnersToAdd = round.WinningParticipantIds.Select( wp => new RoundWinner { Round = round.Id, Participant = wp }); foreach (var winner in winnersToAdd) { await transactionalQueryExecutor .NewQuery( @"INSERT INTO [dbo].[RoundWinner] ([Round] ,[Participant]) VALUES (@Round ,@Participant)") .WithCommandType(CommandType.Text) .WithParameters(new { Round = winner.Round, Participant = winner.Participant }) .ExecuteAsync(); } transactionalQueryExecutor.Commit(); } catch { transactionalQueryExecutor.Rollback(); throw; } } }
public async Task UpdateSeason(SeasonEditModel season) { if (season == null) { throw new ArgumentNullException(nameof(season)); } // Find record to update var seasonToUpdate = _context.Season.Single(s => s.Id == season.Id); // Update own props seasonToUpdate.Label = season.Label; seasonToUpdate.WinningTeam = season.WinningTeamId; _context.Update(seasonToUpdate); await _context.SaveChangesAsync(); // Update winners using (var transactionalQueryExecutor = _queryExecutor.BeginTransaction()) { try { await transactionalQueryExecutor .NewQuery("DELETE FROM [dbo].[SeasonWinner] WHERE [Season]=@Season") .WithCommandType(CommandType.Text) .WithParameters(new { Season = season.Id }) .ExecuteAsync(); var winnersToAdd = season.WinningParticipantIds.Select( wp => new SeasonWinner { Season = season.Id, Participant = wp }); foreach (var winner in winnersToAdd) { await transactionalQueryExecutor .NewQuery( @" INSERT INTO [dbo].[SeasonWinner] ([Season] ,[Participant]) VALUES (@Season ,@Participant)") .WithCommandType(CommandType.Text) .WithParameters(new { Season = winner.Season, Participant = winner.Participant }) .ExecuteAsync(); } transactionalQueryExecutor.Commit(); } catch { transactionalQueryExecutor.Rollback(); throw; } } }