public static void Main()
        {
            using (var db = new FootballEntities())
            {
                var xml = XDocument.Load("../../../Helper.Files/leagues-and-teams.xml");
                var counter = 1;
                foreach (var league in xml.Descendants("league"))
                {
                    Console.WriteLine("Processing league #{0} ...", counter++);
                    var currentLeague = GetOrCreateLeague(league, db);
                    foreach (var team in league.Descendants("team"))
                    {
                        if (team.Attribute("name") == null)
                        {
                            Console.WriteLine("Missing team name attribute.");
                            continue;
                        }

                        var currentTeam = GetOrCreateTeam(team, db);
                        AddTeamToLeague(currentTeam, currentLeague);
                    }

                    db.SaveChanges();
                    Console.WriteLine();
                }
            }
        }
        private static string GetRandomTeam(string leagueName, string opponentTeam = null)
        {
            using (var db = new FootballEntities())
            {
                League currentLeague = null;
                if (leagueName != "no league")
                {
                    currentLeague = db.Leagues.FirstOrDefault(l => l.LeagueName == leagueName);
                }

                string randomTeam;
                if (currentLeague != null)
                {
                    var randomNumber = RandomGenerator.Next(currentLeague.Teams.Count());
                    randomTeam = currentLeague.Teams.OrderBy(t => t.TeamName).Skip(randomNumber).First().TeamName;
                }
                else
                {
                    var randomNumber = RandomGenerator.Next(db.Teams.Count());
                    randomTeam = db.Teams.OrderBy(t => t.TeamName).Skip(randomNumber).First().TeamName;
                }

                if (randomTeam == opponentTeam)
                {
                    return GetRandomTeam(leagueName, opponentTeam);
                }

                return randomTeam;
            }
        }
 public static void Main()
 {
     using (var db = new FootballEntities())
     {
         foreach (var team in db.Teams)
         {
             Console.WriteLine(team.TeamName);
         }
     }
 }
        public static void Main()
        {
            using (var db = new FootballEntities())
            {
                var leaguesAndTeams = db.Leagues
                    .OrderBy(l => l.LeagueName)
                    .Select(l => new
                    {
                        leagueName = l.LeagueName,
                        teams = l.Teams.OrderBy(t => t.TeamName).Select(t => t.TeamName)
                    })
                    .ToList();

                var json = JsonConvert.SerializeObject(leaguesAndTeams, Formatting.Indented);
                File.WriteAllText("../../../Helper.Files/leagues-and-teams.json", json);
            }
        }
        private static Team GetOrCreateTeam(XElement team, FootballEntities db)
        {
            var teamName = team.Attribute("name").Value;
            string countryName = null;
            if (team.Attribute("country") != null)
            {
                countryName = team.Attribute("country").Value;
            }

            Team currentTeam;
            if (countryName != null)
            {
                currentTeam = db.Teams.FirstOrDefault(t => t.TeamName == teamName && t.Country.CountryName == countryName);
            }
            else
            {
                currentTeam = db.Teams.FirstOrDefault(t => t.TeamName == teamName);
            }

            if (currentTeam == null)
            {
                if (countryName != null)
                {
                    var country = db.Countries.FirstOrDefault(c => c.CountryName == countryName);
                    currentTeam = new Team { TeamName = teamName, Country = country };
                }
                else
                {
                    currentTeam = new Team { TeamName = teamName };
                }

                db.Teams.Add(currentTeam);
                Console.WriteLine("Created team: {0} ({1})",
                    currentTeam.TeamName,
                    currentTeam.Country != null ? currentTeam.Country.CountryName : "no country");
            }
            else
            {
                Console.WriteLine("Existing team: {0} ({1})",
                    currentTeam.TeamName,
                    currentTeam.Country != null ? currentTeam.Country.CountryName : "no country");
            }

            return currentTeam;
        }
        public static void Main()
        {
            using (var db = new FootballEntities())
            {
                var internationalMatches = db.InternationalMatches
                    .OrderBy(m => m.MatchDate)
                    .ThenBy(m => m.HomeCountry.CountryName)
                    .ThenBy(m => m.AwayCountry.CountryName)
                    .Select(m => new
                    {
                        m.HomeCountryCode,
                        m.AwayCountryCode,
                        HomeCountry = m.HomeCountry.CountryName,
                        AwayCountry = m.AwayCountry.CountryName,
                        m.MatchDate,
                        m.League.LeagueName,
                        Score = (m.HomeGoals != null && m.AwayGoals != null) ? m.HomeGoals + "-" + m.AwayGoals : null
                    })
                    .ToList();

                var resultXml = new XElement("matches");
                foreach (var match in internationalMatches)
                {
                    var matchXml = new XElement("match");

                    var homeCountryXml = new XElement("home-country", match.HomeCountry);
                    homeCountryXml.SetAttributeValue("code", match.HomeCountryCode);
                    matchXml.Add(homeCountryXml);

                    var awayCountryXml = new XElement("away-country", match.AwayCountry);
                    awayCountryXml.SetAttributeValue("code", match.AwayCountryCode);
                    matchXml.Add(awayCountryXml);

                    if (match.Score != null)
                    {
                        matchXml.Add(new XElement("score", match.Score));
                    }

                    if (match.LeagueName != null)
                    {
                        matchXml.Add(new XElement("league", match.LeagueName));
                    }

                    if (match.MatchDate != null)
                    {
                        if (match.MatchDate.Value.TimeOfDay.Ticks != 0)
                        {
                            matchXml.SetAttributeValue("date-time", match.MatchDate.Value.ToString("dd-MMM-yyyy hh:mm"));
                        }
                        else
                        {
                            matchXml.SetAttributeValue("date", match.MatchDate.Value.ToString("dd-MMM-yyyy"));
                        }
                    }

                    resultXml.Add(matchXml);
                }

                new XDocument(resultXml).Save("../../../Helper.Files/international-matches.xml");
            }
        }
        private static void SaveMatchToDb(string leagueName, string homeTeam, string awayTeam, int homeTeamGoals, int awayTeamGoals, DateTime matchDate)
        {
            using (var db = new FootballEntities())
            {
                db.TeamMatches.Add(new TeamMatch
                {
                    League = db.Leagues.FirstOrDefault(l => l.LeagueName == leagueName),
                    HomeTeam = db.Teams.FirstOrDefault(t => t.TeamName == homeTeam),
                    AwayTeam = db.Teams.FirstOrDefault(t => t.TeamName == awayTeam),
                    HomeGoals = homeTeamGoals,
                    AwayGoals = awayTeamGoals,
                    MatchDate = matchDate
                });

                db.SaveChanges();
            }
        }
        private static League GetOrCreateLeague(XContainer league, FootballEntities db)
        {
            League currentLeague = null;
            var leagueNameNode = league.Descendants("league-name").SingleOrDefault();
            if (leagueNameNode != null)
            {
                currentLeague = db.Leagues.FirstOrDefault(l => l.LeagueName == leagueNameNode.Value);
                if (currentLeague == null)
                {
                    currentLeague = new League { LeagueName = leagueNameNode.Value };
                    db.Leagues.Add(currentLeague);
                    Console.WriteLine("Created league: {0}", currentLeague.LeagueName);
                }
                else
                {
                    Console.WriteLine("Existing league: {0}", currentLeague.LeagueName);
                }
            }

            return currentLeague;
        }