Ejemplo n.º 1
0
        public async Task <T> AddAsync(T entity)
        {
            _dbContext.Set <T>().Add(entity);
            await _dbContext.SaveChangesAsync();

            return(entity);
        }
Ejemplo n.º 2
0
        public static async Task SeedAsync(CarsContext carsContext,
                                           ILoggerFactory loggerFactory, int?retry = 0)
        {
            int retryForAvailability = retry.Value;

            try
            {
                // Seed Areas and Garages
                if (!carsContext.Areas.Any())
                {
                    IEnumerable <Area> areas = GetPreconfiguredAreas();
                    carsContext.Areas.AddRange(areas);

                    foreach (Area area in areas)
                    {
                        carsContext.Garages.AddRange(GetPreconfiguredGarages(area));
                    }

                    await carsContext.SaveChangesAsync();
                }

                if (!carsContext.CarCategories.Any())
                {
                    carsContext.CarCategories.AddRange(GetPreconfiguredCarCategories());
                    await carsContext.SaveChangesAsync();
                }

                if (!carsContext.CarImpactClasses.Any())
                {
                    carsContext.CarImpactClasses.AddRange(GetPreconfiguredCarImpactClasses());
                    await carsContext.SaveChangesAsync();
                }

                if (!carsContext.CarProbabilityClasses.Any())
                {
                    carsContext.CarProbabilityClasses.AddRange(GetPreconfiguredCarProbabilityClasses());
                    await carsContext.SaveChangesAsync();
                }
            }
            catch (Exception ex)
            {
                if (retryForAvailability < 10)
                {
                    retryForAvailability++;
                    var log = loggerFactory.CreateLogger <CarsContextSeed>();
                    log.LogError(ex.Message);
                    await SeedAsync(carsContext, loggerFactory, retryForAvailability);
                }
            }
        }