public static async Task SeedAsync(FootballManagerContext catalogContext,
                                           ILoggerFactory loggerFactory, int?retry = 0)
        {
            int retryForAvailability = retry.Value;

            try
            {
                if (!await catalogContext.Players.AnyAsync())
                {
                    var teams = GetPreconfiguredTeams();
                    foreach (var team in teams)
                    {
                        catalogContext.Teams.Add(team);
                        var players = GeneratePreconfiguredTeamRooster();
                        catalogContext.Players.AddRange(players);
                        await catalogContext.SaveChangesAsync();

                        // Add the players to the team
                        foreach (var player in players)
                        {
                            team.AddPlayer(player);
                        }
                        await catalogContext.SaveChangesAsync();

                        // Add players to the formation
                        team.Formation.AddPlayer(players.First(x => x.Role == PlayerRole.Goalkeeper));

                        foreach (var defender in players.Where(x => x.Role == PlayerRole.Defender).Take(4))
                        {
                            team.Formation.AddPlayer(defender);
                        }

                        foreach (var midfielder in players.Where(x => x.Role == PlayerRole.Midfielder).Take(4))
                        {
                            team.Formation.AddPlayer(midfielder);
                        }

                        foreach (var attacker in players.Where(x => x.Role == PlayerRole.Attacker).Take(2))
                        {
                            team.Formation.AddPlayer(attacker);
                        }

                        await catalogContext.SaveChangesAsync();
                    }
                }
            }
            catch (Exception ex)
            {
                if (retryForAvailability < 10)
                {
                    retryForAvailability++;
                    var log = loggerFactory.CreateLogger <FootballManagerContextSeed>();
                    log.LogError(ex.Message);
                    await SeedAsync(catalogContext, loggerFactory, retryForAvailability);
                }
                throw;
            }
        }
示例#2
0
 public TeamRepository(FootballManagerContext dbContext) : base(dbContext)
 {
 }
示例#3
0
 public EfRepository(FootballManagerContext dbContext)
 {
     _dbContext = dbContext;
 }
示例#4
0
 public PlayerRepository(FootballManagerContext dbContext) : base(dbContext)
 {
 }