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();
        }
        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 internationalMatches = context.InternationalMatches
                .OrderBy(m => m.MatchDate)
                .ThenBy(m => m.Country1)
                .ThenBy(m => m.Country)
                .Select(m => new
                {
                    m.HomeCountryCode,
                    homeCountryName = m.Country1.CountryName,
                    m.AwayCountryCode,
                    awayCountryName = m.Country.CountryName,
                    m.MatchDate,
                    m.League,
                    m.HomeGoals,
                    m.AwayGoals
                })
                .ToList();

            var xmlRoot = new XElement("matches");
            foreach (var match in internationalMatches)
            {
                var xmlMatch = new XElement("match");
                if (match.MatchDate != null)
                {
                    DateTime date = DateTime.Parse(match.MatchDate.ToString());
                    if (date.Hour == 0 && date.Minute == 0)
                    {
                        var dateAttr = new XAttribute("date", date.ToString("dd-MMM-yyyy"));
                        xmlMatch.Add(dateAttr);
                    }
                    else
                    {
                        var dateAttr = new XAttribute("date", date.ToString("dd-MMM-yyyy hh:ss"));
                        xmlMatch.Add(dateAttr);
                    }

                }

                var homeCountry = new XElement("home-country", match.HomeCountryCode);
                var homeCountryCodeAtt = new XAttribute("code", match.HomeCountryCode);
                homeCountry.Add(homeCountryCodeAtt);
                xmlMatch.Add(homeCountry);

                var awayCountry = new XElement("home-country", match.awayCountryName);
                var awayCountryCodeAtt = new XAttribute("code", match.AwayCountryCode);
                awayCountry.Add(awayCountryCodeAtt);
                xmlMatch.Add(awayCountry);

                if (match.HomeGoals != null && match.AwayGoals != null)
                {
                    var scores = string.Format("{0}-{1}", match.HomeGoals, match.AwayGoals);
                    var scoreXml = new XElement("score", scores);
                    xmlMatch.Add(scoreXml);
                }

                if (match.League != null)
                {
                    var leagueXml = new XElement("league", match.League.LeagueName);
                    xmlMatch.Add(leagueXml);
                }

                xmlRoot.Add(xmlMatch);
            }
            var xmlDoc = new XDocument(xmlRoot);
            xmlDoc.Save("international-matches.xml");

            Console.WriteLine("File international-matches.xml was saved in bin/Debug");
        }