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

            return(entity);
        }
Esempio n. 2
0
        public static async Task SeedAsync(EShopContext eShopContext,
                                           ILoggerFactory loggerFactory, int?retry = 0)
        {
            int retryForAvailability = retry.Value;

            try
            {
                eShopContext.Database.Migrate();

                if (!eShopContext.Suppliers.Any())
                {
                    eShopContext.Suppliers.AddRange(
                        GetPreconfiguredSuppliers());

                    await eShopContext.SaveChangesAsync();
                }

                if (!eShopContext.Categories.Any())
                {
                    eShopContext.Categories.AddRange(
                        GetPreconfiguredCategories());

                    await eShopContext.SaveChangesAsync();
                }

                if (!eShopContext.Products.Any())
                {
                    eShopContext.Products.AddRange(
                        GetPreconfiguredProducts());

                    await eShopContext.SaveChangesAsync();
                }
            }
            catch (Exception ex)
            {
                if (retryForAvailability < 10)
                {
                    retryForAvailability++;
                    var log = loggerFactory.CreateLogger <EShopContextSeed>();
                    log.LogError(ex.Message);
                    await SeedAsync(eShopContext, loggerFactory, retryForAvailability);
                }
            }
        }