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; } }
public async Task <Product> AddAsync(Product product) { _context.Products.Add(product); await _context.SaveChangesAsync(); return(product); }