public async Task <int> Complete() { return(await _dzDbContext.SaveChangesAsync()); }
public static async Task SeedDzDb(DzDbContext context, ILoggerFactory loggerFactory) { try { if (!context.ProductBrands.Any()) { var brandsData = File.ReadAllText("../DzShopping.Infrastructure/SeedData/brands.json"); var brands = JsonSerializer.Deserialize <List <ProductBrand> >(brandsData); foreach (var item in brands) { context.ProductBrands.Add(item); } await context.SaveChangesAsync(); } if (!context.ProductTypes.Any()) { var typesData = File.ReadAllText("../DzShopping.Infrastructure/SeedData/types.json"); var types = JsonSerializer.Deserialize <List <ProductType> >(typesData); foreach (var item in types) { context.ProductTypes.Add(item); } await context.SaveChangesAsync(); } if (!context.Products.Any()) { var productsData = File.ReadAllText("../DzShopping.Infrastructure/SeedData/products.json"); var products = JsonSerializer.Deserialize <List <Product> >(productsData); foreach (var item in products) { context.Products.Add(item); } await context.SaveChangesAsync(); } if (!context.DeliveryMethods.Any()) { var deliveryMethodsData = File.ReadAllText("../DzShopping.Infrastructure/SeedData/delivery.json"); var deliveryMethods = JsonSerializer.Deserialize <List <DeliveryMethod> >(deliveryMethodsData); foreach (var item in deliveryMethods) { context.DeliveryMethods.Add(item); } await context.SaveChangesAsync(); } } catch (Exception ex) { var logger = loggerFactory.CreateLogger <DzContextSeed>(); logger.LogError(ex.Message); } }