private static void SeedCategoriesData(IApplicationBuilder app) { using var serviceScope = app.ApplicationServices.GetService <IServiceScopeFactory>().CreateScope(); var categoryService = serviceScope.ServiceProvider.GetRequiredService <ICategoryService>(); if (categoryService.CountCategories() == 0) { foreach (var category in new CategoriesSeed().GetCategories()) { categoryService.CreateCategory(category); var subCategories = new CategoriesSeed() .GetSubCategories() .Where(d => d.Category.Id == category.Id) .ToList(); foreach (var subCategory in subCategories) { categoryService.CreateSubCategory(subCategory, category); } } } }
public static void EnsureSeeded(this CatalogContext context, IConfiguration configuration) { BrandsSeed.SeedBrands(context, configuration); CategoriesSeed.SeedCategories(context); CategoryImagesSeed.SeedCategoryImages(context); }