Пример #1
0
        public static void Main()
        {
            var context = new FootballEntities();
            int count   = 0;

            var xmlDocument = XDocument.Load("../../leagues-and-teams.xml");
            var leagueNodes = xmlDocument.XPathSelectElements("/leagues-and-teams/league");

            foreach (var leagueNode in leagueNodes)
            {
                Console.WriteLine("Processing league #{0} ...", ++count);
                League league     = null;
                string leagueName = null;

                if (leagueNode.Element("league-name") != null)
                {
                    leagueName = leagueNode.Element("league-name").Value;

                    league = context.Leagues.Where(l => l.LeagueName == leagueName).FirstOrDefault();

                    if (league == null)
                    {
                        league = new League()
                        {
                            LeagueName = leagueName
                        };
                        context.Leagues.Add(league);
                        context.SaveChanges();

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

                var matchNodes = leagueNode.XPathSelectElements("teams/team");
                foreach (var matchNode in matchNodes)
                {
                    string teamName    = matchNode.Attribute("name").Value;
                    string teamCountry = null;

                    if (matchNode.Attribute("country") != null)
                    {
                        teamCountry = matchNode.Attribute("country").Value;
                    }

                    var team = context.Teams.Where(t => t.TeamName == teamName).FirstOrDefault();

                    if (team == null)
                    {
                        team = new Team()
                        {
                            TeamName    = teamName,
                            CountryCode = context.Countries.Where(c => c.CountryName == teamCountry).Select(c => c.CountryCode).FirstOrDefault()
                        };

                        context.Teams.Add(team);
                        context.SaveChanges();

                        Console.WriteLine("Created team: {0} ({1})", teamName, teamCountry);
                    }
                    else
                    {
                        Console.WriteLine("Existing team: {0} ({1})", teamName, teamCountry);
                    }

                    if (leagueName != null)
                    {
                        if (league.Teams.Contains(team))
                        {
                            Console.WriteLine("Existing team in league: {0} belongs to {1}",
                                              leagueName,
                                              teamName);
                        }
                        else
                        {
                            team.Leagues.Add(league);
                            context.SaveChanges();

                            Console.WriteLine("Added team to league: {0} to league {1}",
                                              leagueName,
                                              teamName);
                        }
                    }
                }
            }
        }
Пример #2
0
        static void Main()
        {
            var    footballContext = new FootballEntities();
            Random random          = new Random();
            int    count           = 0;

            List <Team>   allTeams   = footballContext.Teams.Include("Leagues").ToList();
            List <League> allLeagues = footballContext.Leagues.ToList();

            XDocument xmlDocument = XDocument.Load("../../generate-matches.xml");
            IEnumerable <XElement> generateNodes = xmlDocument.XPathSelectElements("/generate-random-matches/generate");

            foreach (XElement generateNode in generateNodes)
            {
                Console.WriteLine("\r\nProcessing request #{0} ...", ++count);

                int      generateCount = 10;
                int      maxGoals      = 5;
                DateTime startDate     = Convert.ToDateTime("1-Jan-2000");
                DateTime endDate       = Convert.ToDateTime("31-Dec-2015");
                League   league        = null;
                string   leagueName    = null;
                int?     leagueId      = null;

                if (generateNode.Attribute("generate-count") != null)
                {
                    generateCount = int.Parse(generateNode.Attribute("generate-count").Value);
                }

                if (generateNode.Attribute("max-goals") != null)
                {
                    maxGoals = int.Parse(generateNode.Attribute("max-goals").Value);
                }

                if (generateNode.Element("league") != null)
                {
                    leagueName = generateNode.Element("league").Value;
                    league     = allLeagues.Where(l => l.LeagueName == leagueName).FirstOrDefault();

                    if (league != null)
                    {
                        leagueId = league.Id;
                    }
                }

                if (generateNode.Element("start-date") != null)
                {
                    startDate = Convert.ToDateTime(generateNode.Element("start-date").Value);
                }

                if (generateNode.Element("end-date") != null)
                {
                    endDate = Convert.ToDateTime(generateNode.Element("end-date").Value);
                }

                for (int i = 0; i < generateCount; i++)
                {
                    TimeSpan timeSpan    = endDate - startDate;
                    TimeSpan newSpan     = new TimeSpan(0, random.Next(0, (int)timeSpan.TotalMinutes), 0);
                    DateTime currentDate = startDate + newSpan;

                    var possibleTeams = allTeams;

                    if (league != null)
                    {
                        possibleTeams = possibleTeams
                                        .Where(t => t.Leagues.Select(l => l.Id).Contains(league.Id))
                                        .ToList();
                    }

                    var homeTeam = possibleTeams[random.Next(possibleTeams.Count())];
                    var awayTeam = possibleTeams[random.Next(possibleTeams.Count())];

                    var homeGoals = random.Next(maxGoals);
                    var awayGoals = random.Next(maxGoals);

                    TeamMatch currentTeamMatch = new TeamMatch()
                    {
                        HomeTeamId = homeTeam.Id,
                        AwayTeamId = awayTeam.Id,
                        HomeGoals  = homeGoals,
                        AwayGoals  = awayGoals,
                        MatchDate  = currentDate,
                        LeagueId   = leagueId
                    };

                    footballContext.TeamMatches.Add(currentTeamMatch);

                    Console.WriteLine("{0} {1}: - {2}: {3}-{4} ({5})",
                                      currentDate.ToString("dd-MMM-yyyy"),
                                      homeTeam.TeamName,
                                      awayTeam.TeamName,
                                      homeGoals,
                                      awayGoals,
                                      leagueName ?? "no league");
                }
            }

            footballContext.SaveChanges();
        }