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

            return(entity);
        }
Example #2
0
        public static async Task SeedAsync(CatalogoContext catalogoContext,
                                           ILoggerFactory loggerFactory, int?retry = 0)
        {
            int retryForAvailability = retry.Value;

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

                if (!catalogoContext.CatalogoMarcas.Any())
                {
                    catalogoContext.CatalogoMarcas.AddRange(
                        GetPreconfiguredCatalogoMarcas());

                    await catalogoContext.SaveChangesAsync();
                }

                if (!catalogoContext.CatalogosTipos.Any())
                {
                    catalogoContext.CatalogosTipos.AddRange(
                        GetPreconfiguredCatalogosTipos());

                    await catalogoContext.SaveChangesAsync();
                }

                if (!catalogoContext.CatalogoItems.Any())
                {
                    catalogoContext.CatalogoItems.AddRange(
                        GetPreconfiguredItems());

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