public async Task SeedAsync(LibraaryDbContext dbContext, IServiceProvider serviceProvider)
        {
            if (dbContext == null)
            {
                throw new ArgumentNullException(nameof(dbContext));
            }

            if (serviceProvider == null)
            {
                throw new ArgumentNullException(nameof(serviceProvider));
            }

            var logger = serviceProvider.GetService <ILoggerFactory>().CreateLogger(typeof(LibraaryDbContextSeeder));

            var seeders = new List <ISeeder>
            {
                new RolesSeeder(),
                new CategoriesSeeder(),
                new UsersSeeder(),
                new AuthorsSeeder(),
                new PublishersSeeder()
            };

            foreach (var seeder in seeders)
            {
                await seeder.SeedAsync(dbContext, serviceProvider);

                await dbContext.SaveChangesAsync();

                logger.LogInformation($"Seeder {seeder.GetType().Name} done.");
            }
        }
        private static async Task SeedCategoryAsync(LibraaryDbContext context, string[] categories)
        {
            foreach (var category in categories)
            {
                if (!context.Categories.Any(c => c.CategoryName == category))
                {
                    await context.Categories.AddAsync(new Category()
                    {
                        CategoryName = category
                    });
                }
            }

            await context.SaveChangesAsync();
        }
Esempio n. 3
0
        private static async Task SeedCategoryAsync(LibraaryDbContext context, string[] publishers)
        {
            foreach (var publisher in publishers)
            {
                var urlAddress = publisher.Split('|')[1];
                var name       = publisher.Split('|')[0];
                if (!context.Publishers.Any(p => p.Name == name))
                {
                    await context.Publishers.AddAsync(new Publisher()
                    {
                        Name       = name,
                        URLAddress = urlAddress
                    });
                }
            }

            await context.SaveChangesAsync();
        }
Esempio n. 4
0
        private static async Task SeedCategoryAsync(LibraaryDbContext context, string[] authors)
        {
            foreach (var author in authors)
            {
                var firstName   = author.Split()[0];
                var lastName    = author.Split()[1];
                var nationality = author.Split()[2];

                if (!context.Authors.Any(a => a.FirstName == firstName && a.LastName == lastName))
                {
                    await context.Authors.AddAsync(new Author()
                    {
                        FirstName   = firstName,
                        LastName    = lastName,
                        Nationality = nationality
                    });
                }
            }

            await context.SaveChangesAsync();
        }