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

            return(entity);
        }
Пример #2
0
        public static async Task SeedAsync(CloudContext CloudContext,
                                           ILoggerFactory loggerFactory, int?retry = 0)
        {
            int retryForAvailability = retry.Value;

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

                if (!CloudContext.CatalogBrands.Any())
                {
                    CloudContext.CatalogBrands.AddRange(
                        GetPreconfiguredCatalogBrands());

                    await CloudContext.SaveChangesAsync();
                }

                if (!CloudContext.CatalogTypes.Any())
                {
                    CloudContext.CatalogTypes.AddRange(
                        GetPreconfiguredCatalogTypes());

                    await CloudContext.SaveChangesAsync();
                }

                if (!CloudContext.CatalogItems.Any())
                {
                    CloudContext.CatalogItems.AddRange(
                        GetPreconfiguredItems());

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