コード例 #1
0
ファイル: RazorCRUDSeed.cs プロジェクト: Tabrita/RazorCRUDApp
        public static async Task SeedAsync(RazorCRUDContext context, ILoggerFactory loggerFactory, int?retry = 0)
        {
            int retryForAvailability = retry.Value;

            try
            {
                // TODO: Only run this if using a real database
                //context.Database.Migrate();
                //context.Database.EnsureCreated();
                if (!context.Categories.Any())
                {
                    context.Categories.AddRange(GetPreconfiguredCategories());
                    await context.SaveChangesAsync();
                }
                if (!context.Products.Any())
                {
                    context.Products.AddRange(GetPreconfiguredProducts());
                    await context.SaveChangesAsync();
                }
            }
            catch (Exception exception)
            {
                if (retryForAvailability < 10)
                {
                    retryForAvailability++;
                    var log = loggerFactory.CreateLogger <RazorCRUDSeed>();
                    log.LogError(exception.Message);
                    await SeedAsync(context, loggerFactory, retryForAvailability);
                }
                throw;
            }
        }
コード例 #2
0
        public async Task <Product> AddAsync(Product product)
        {
            _context.Products.Add(product);
            await _context.SaveChangesAsync();

            return(product);
        }