static void Main() { var context = new FootballEntities(); var teams = context.Teams; var xmlDoc = XDocument.Load(@"..\..\generate-matches.xml"); var randomMatches = xmlDoc.XPathSelectElements("/generate-random-matches/generate"); int count = 5; int maxGoals = 10; int processingCounter = 1; string league = "no league"; DateTime startDate = new DateTime(2000, 1, 1); DateTime endDate = new DateTime(2015, 12, 31); foreach (var match in randomMatches) { Console.WriteLine("Processing request #{0} ...", processingCounter); processingCounter++; if (match.Attribute("generate-count") != null) { count = int.Parse(match.Attribute("generate-count").Value); } if (match.Attribute("max-goals") != null) { maxGoals = int.Parse(match.Attribute("max-goals").Value); } if (match.Descendants("start-date").FirstOrDefault() != null) { startDate = FormatDateTimeAsReq(match.Descendants("start-date").First().Value); } if (match.Descendants("end-date").FirstOrDefault() != null) { endDate = FormatDateTimeAsReq(match.Descendants("end-date").First().Value); } if (match.Descendants("league").FirstOrDefault() != null) { league = match.Descendants("league").First().Value; } for (int i = 0; i < count; i++) { var currentMatch = new TeamMatch(); DateTime matchDate = GenerateRandomDateInRange(startDate, endDate); int homeTeamScores = GenerateRandomNumber(maxGoals); int awayTeamScores = GenerateRandomNumber(maxGoals); int homeTeamId = 0; int awayTeamId = 0; do { homeTeamId = GenerateRandomNumber(teams.Count()); awayTeamId = GenerateRandomNumber(teams.Count()); } while (homeTeamId == awayTeamId); currentMatch.AwayGoals = awayTeamScores; currentMatch.AwayTeamId = awayTeamId; currentMatch.HomeGoals = homeTeamScores; currentMatch.HomeTeamId = homeTeamId; currentMatch.MatchDate = matchDate; if (league != "no league") { var currentLeague = context.Leagues.FirstOrDefault(l => l.LeagueName == league); if (currentLeague != null) { currentMatch.League = currentLeague; } } var homeTeamName = context.Teams.Find(homeTeamId).TeamName; var awayTeamName = context.Teams.Find(awayTeamId).TeamName; PrintOnConsole(homeTeamName, awayTeamName, homeTeamScores, awayTeamScores, league, matchDate); context.TeamMatches.Add(currentMatch); } } context.SaveChanges(); }
static void Main() { var context = new FootballEntities(); var xmlDoc = XDocument.Load(@"..\..\leagues-and-teams.xml"); var leaguesNodes = xmlDoc.XPathSelectElements("/leagues-and-teams/league"); int leagueCounter = 1; foreach (var league in leaguesNodes) { Console.WriteLine(string.Format("Processing league #{0} ...", leagueCounter)); var leagueName = league.Descendants("league-name").FirstOrDefault(); var teams = league.Descendants("teams").FirstOrDefault(); var currentLeague = new League(); if (leagueName != null) { var currentLeagueInDb = context.Leagues.FirstOrDefault(l => l.LeagueName == leagueName.Value); if (currentLeagueInDb == null) { currentLeague.LeagueName = leagueName.Value; Console.WriteLine("Created league: {0}", leagueName.Value); context.Leagues.Add(currentLeague); } else { currentLeague = currentLeagueInDb; Console.WriteLine("Existing league: {0}", leagueName.Value); } } var leagueTeams = currentLeague.Teams; if (teams != null && teams.Descendants("team").FirstOrDefault() != null) { foreach (var team in teams.Descendants("team")) { var currentTeam = new Team(); var teamName = team.Attribute("name").Value; var teamCountryAttr = team.Attribute("country"); var teamInDb = context.Teams.FirstOrDefault(t => t.TeamName == teamName); if (teamInDb != null && teamCountryAttr != null && teamInDb.Country.CountryName != teamCountryAttr.Value) { teamInDb = null; } if (teamInDb == null) { var teamCountryCode = (teamCountryAttr != null) ? context.Countries.FirstOrDefault(c => c.CountryName == teamCountryAttr.Value) : null; currentTeam.TeamName = teamName; if (teamCountryCode != null) { currentTeam.CountryCode = teamCountryCode.CountryCode; } Console.WriteLine("Created team: {0}", teamName); context.Teams.Add(currentTeam); } else { currentTeam = teamInDb; Console.WriteLine("Existing team: {0}", teamName); } if (leagueTeams.Contains(currentTeam)) { Console.WriteLine("Existing team: {0}", teamName); } else { leagueTeams.Add(currentTeam); Console.WriteLine("Added team to league: {0} to league {1}", teamName, currentLeague.LeagueName); } } } leagueCounter++; Console.WriteLine(); } context.SaveChanges(); }