Ejemplo n.º 1
0
        public async Task SeedAsync(ForumWMADbContext dbContext, IServiceProvider serviceProvider)
        {
            if (dbContext == null)
            {
                throw new ArgumentNullException(nameof(dbContext));
            }

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

            var logger = serviceProvider.GetRequiredService <ILoggerFactory>().CreateLogger(typeof(ForumWMADbContextSeeder));

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

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

                await dbContext.SaveChangesAsync();

                logger.LogInformation($"Seeder {seeder.GetType().Name} done.");
            }
        }
Ejemplo n.º 2
0
        public async Task <string> Create(string name, string title, string description, string imageUrl)
        {
            var category = new Category()
            {
                Name        = name,
                Title       = title,
                Description = description,
                ImageUrl    = imageUrl,
                CreatedOn   = DateTime.UtcNow,
            };

            await context.Categories.AddAsync(category);

            await context.SaveChangesAsync();

            return(category.Id);
        }