Esempio n. 1
0
        public void Seed(SmartStoreContext context, DbOptions dbOptions, ILogger <SmartStoreContextSeeder> logger)
        {
            if (!dbOptions.Seed)
            {
                logger.LogInformation("Seed has not been activated.");
                return;
            }

            var policy = CreatePolicy(logger, nameof(SmartStoreContextSeeder));

            policy.ExecuteAsync(async() =>
            {
                await using (context)
                {
                    await context.Database.MigrateAsync();

                    var manufacturers = GetPredefinedManufacturers();
                    if (!context.Manufacturers.Any())
                    {
                        await context.AddRangeAsync(manufacturers);
                    }

                    await context.SaveChangesAsync();

                    var categories = GetPredefinedCategories();
                    if (!context.Categories.Any())
                    {
                        await context.AddRangeAsync(categories);
                    }

                    await context.SaveChangesAsync();

                    var products = GetPredefinedProduct();
                    if (!context.Products.Any())
                    {
                        await context.AddRangeAsync(products);
                        await context.SaveChangesAsync();

                        LinkProductsWithManufacturersAndCategories(ref products, categories, manufacturers);

                        await context.SaveChangesAsync();
                    }
                }
            }).Wait();
        }