示例#1
0
        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 ProductRepository(RazorCRUDContext context)
 {
     _context = context;
 }