/// <summary> /// Fill the matches with data taken from XML file /// </summary> /// <param name="matches">Gets a collection of matches</param> public void FillMatchesFromXml(ICollection<Match> matches) { var ctx = new FootballContext(); using (ctx) { foreach (var match in matches) { ctx.Matches.Add(match); } ctx.SaveChanges(); } }
private async void GetMongoData_Click(object sender, EventArgs e) { try { var repo = new MongoDbRepository(); var teams = (await repo.GetTeamsData()).ToList(); var stadiums = (await repo.GetStadiumsData()).ToList(); var ctx = new FootballContext(); using (ctx) { foreach (var team in teams) { if (!ctx.Teams.Any(pl => pl.Id == team.Id)) { ctx.Teams.Add(team); } } foreach (var stadium in stadiums) { if (!ctx.Stadiums.Any(pl => pl.Id == stadium.Id)) { ctx.Stadiums.Add(stadium); } } ctx.SaveChanges(); } MessageBox.Show( "The teams, couches, stadiums and towns are inserted", "Teams insert", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (DataException) { MessageBox.Show( "No connection to MongoDb!!!", "MongoDb", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Fill the players with data taken from ZIP file /// </summary> /// <param name="teams">Gets a dictionary with KEY of type string and VALUE of type List<Player></param> public void FillPlayersFromZip(Dictionary<string, List<Player>> teams) { var ctx = new FootballContext(); using (ctx) { foreach (var team in teams) { var team1 = ctx.Teams.FirstOrDefault(t => t.Name == team.Key); if (team1 == null) { continue; } foreach (var player in team.Value) { var currPlayer = ctx.Players .SingleOrDefault(pl => pl.Number == player.Number && pl.Team.Name == team.Key); if (currPlayer == null) { player.Team = team1; ctx.Players.Add(player); } else { currPlayer.Salary = player.Salary; currPlayer.Position = player.Position; currPlayer.FirstName = player.FirstName; currPlayer.LastName = player.LastName; } } } ctx.SaveChanges(); } }