Пример #1
0
        public async Task <T> AddAsync(T entity)
        {
            await _dbContext.Set <T>().AddAsync(entity);

            await _dbContext.SaveChangesAsync();

            return(entity);
        }
Пример #2
0
        public async static Task SeedAsync(GigContext gigContext, ILoggerFactory loggerFactory, int?retry = 0)
        {
            var log = loggerFactory.CreateLogger <GigContextSeed>();

            int retryForAvailability = retry.Value;

            try
            {
                log.LogInformation("Applying migrations...");

                // TODO: Only run this if using a real database
                gigContext.Database.Migrate();

                if (!gigContext.Gigs.Any())
                {
                    log.LogInformation("Adding data - seeding...");
                    gigContext.Gigs.AddRange(
                        GetPreconfiguredGigs());

                    await gigContext.SaveChangesAsync();
                }
            }
            catch (Exception ex)
            {
                if (retryForAvailability < 10)
                {
                    retryForAvailability++;
                    log.LogError(ex.Message);
                    await SeedAsync(gigContext, loggerFactory, retryForAvailability);
                }
                throw;
            }
        }