private static void CreateTeamIfNotExist(IEnumerable<XElement> teamNodes, League league, FootballEntities context)
        {
            foreach (var teamNode in teamNodes)
            {
                string teamName = teamNode.Attribute("name").Value;
                var country = teamNode.Attribute("country");
                string countryName = null;
                if (country != null)
                {
                    countryName = country.Value;
                }

                var team = context.Teams
                    .Include(t => t.Leagues)
                    .FirstOrDefault(t => t.TeamName == teamName && t.Country.CountryName == countryName);
                if (team != null)
                {
                    Console.WriteLine("Existing team: {0} ({1})", team.TeamName, countryName ?? "no country");
                }
                else
                {
                    team = new Team()
                    {
                        TeamName = teamName,
                        Country = context.Countries.FirstOrDefault(c => c.CountryName == countryName)
                    };

                    context.Teams.Add(team);
                    context.SaveChanges();
                    Console.WriteLine("Created team: {0} ({1})", team.TeamName, countryName ?? "no country");
                }

                AddTeamToLeague(team, league, context);
            }
        }
        private static League CreateLeagueIfNotExist(XElement leagueNode, FootballEntities context)
        {
            League league = null;
            var leagueNameNode = leagueNode.Descendants("league-name").FirstOrDefault();
            if (leagueNameNode != null)
            {
                string leagueName = leagueNameNode.Value;
                league = context.Leagues.FirstOrDefault(l => l.LeagueName == leagueName);

                if (league == null)
                {
                    league = new League()
                    {
                        LeagueName = leagueName
                    };

                    context.Leagues.Add(league);
                    context.SaveChanges();

                    Console.WriteLine("Created league: {0}", league.LeagueName);
                }
                else
                {
                    Console.WriteLine("Existing league: {0}", leagueName);
                }
            }

            return league;
        }
        private static void AddTeamToLeague(Team team, League league, FootballEntities context)
        {
            if (league != null)
            {
                if (!team.Leagues.Contains(league))
                {
                    team.Leagues.Add(league);
                    context.SaveChanges();

                    Console.WriteLine("Added team to league: {0} to league {1}", team.TeamName, league.LeagueName);
                }
                else
                {
                    Console.WriteLine("Existing team in league: {0} belongs to {1}", team.TeamName, league.LeagueName);
                }
            }
        }
        private static void CreateTeamsIfNotExists(
           FootballEntities context, IEnumerable<XElement> xTeams, League league)
        {
            foreach (var xTeam in xTeams)
            {
                // Find the team by team name and country name (if exists)
                var teamName = xTeam.Attribute("name").Value;
                var xCountry = xTeam.Attribute("country");
                string countryName = null;
                if (xCountry != null)
                {
                    countryName = xCountry.Value;
                }
                var team = context.Teams
                    .Include(t => t.Leagues)
                    .FirstOrDefault(
                        t => t.TeamName == teamName &&
                        t.Country.CountryName == countryName);

                // Create the team if it does not exists
                if (team != null)
                {
                    Console.WriteLine("Existing team: {0} ({1})",
                        team.TeamName, countryName ?? "no country");
                }
                else
                {
                    // Create a new team in the DB
                    team = new Team()
                    {
                        TeamName = teamName,
                        Country = context.Countries.FirstOrDefault(
                            c => c.CountryName == countryName),
                    };
                    context.Teams.Add(team);
                    context.SaveChanges();
                    Console.WriteLine("Created team: {0} ({1})",
                        team.TeamName, countryName ?? "no country");
                }

                AddTeamToLeague(context, team, league);
            }
        }
 private static League CreateLeagueIfNotExists(FootballEntities context, XElement xLeague)
 {
     League league = null;
     var xElementLeagueName = xLeague.Element("league-name");
     if (xElementLeagueName != null)
     {
         string leagueName = xElementLeagueName.Value;
         league = context.Leagues.FirstOrDefault(l => l.LeagueName == leagueName);
         if (league != null)
         {
             Console.WriteLine("Existing league: {0}", leagueName);
         }
         else
         {
             // Create a new league in the DB
             league = new League() { LeagueName = leagueName };
             context.Leagues.Add(league);
             context.SaveChanges();
             Console.WriteLine("Created league: {0}", leagueName);
         }
     }
     return league;
 }
        static void Main(string[] args)
        {
            var context = new FootballEntities();

            XDocument xmlDoc = XDocument.Load("../../leagues-and-teams.xml");

            var leaguesAndTeams =
                from league in xmlDoc.Descendants("league")
                select new
                {
                    LeagueName = league.Element("league-name") != null ? league.Element("league-name").Value : null,
                    Teams = league.Element("teams") != null ? league.Element("teams").Elements() : null
                };

            int countXmlLeagues = 0;

            foreach (var league in leaguesAndTeams)
            {
                League leageToDb = null;
                Console.WriteLine("Processing league #{0}", ++countXmlLeagues);
                if(league.LeagueName != null)
                {
                    leageToDb = context.Leagues.FirstOrDefault(l => l.LeagueName == league.LeagueName);
                    if (leageToDb != null)
                    {
                        Console.WriteLine("Existing league: {0}", league.LeagueName);
                    }
                    else
                    {
                        leageToDb = new League() { LeagueName = league.LeagueName };
                        context.Leagues.Add(leageToDb);
                        Console.WriteLine("Created league: {0}", league.LeagueName);
                    }
                }

                if(league.Teams != null)
                {
                    foreach (var team in league.Teams)
                    {
                        string teamName = team.Attribute("name").Value;
                        string countryName = "";
                        if (team.Attribute("country") != null)
                        {
                            countryName = team.Attribute("country").Value;
                        }

                        Team teamToDb = context.Teams.FirstOrDefault(t => t.TeamName == teamName && t.Country.CountryName == countryName);
                        if(teamToDb == null)
                        {
                            teamToDb = context.Teams.FirstOrDefault(t => t.TeamName == teamName);
                        }

                        if(teamToDb != null)
                        {
                            Console.WriteLine("Existing team: {0} ({1})",
                                teamName, !string.IsNullOrEmpty(countryName) ? countryName : "no country");
                        }
                        else
                        {
                            Country country = context.Countries.FirstOrDefault(c => c.CountryName == countryName);
                            if(!string.IsNullOrEmpty(countryName))
                            {
                                if(country == null)
                                {
                                    country = new Country()
                                    {
                                        CountryName = countryName
                                    };
                                }
                            }
                            teamToDb = new Team()
                                {
                                    TeamName = teamName,
                                    Country = country
                                };
                            context.Teams.Add(teamToDb);

                            Console.WriteLine("Created team: {0} ({1})",
                                teamName, !string.IsNullOrEmpty(countryName) ? countryName : "no country");
                        }

                        if(leageToDb != null)
                        {
                            if(!leageToDb.Teams.Contains(teamToDb))
                            {
                                Console.WriteLine("Existing team in league: {0} belongs to {1}",
                                    teamToDb.TeamName, leageToDb.LeagueName);
                            }
                            else
                            {
                                leageToDb.Teams.Add(teamToDb);
                                Console.WriteLine("Added team to league: {0} to league {1}",
                                    teamToDb.TeamName, leageToDb.LeagueName);
                            }
                        }
                    }
                }

                Console.WriteLine();
                context.SaveChanges();
            }
        }