Пример #1
0
        public async Task SeedAsync(SpravkiDbContext dbContext, IServiceProvider serviceProvider)
        {
            if (dbContext.Users.Any())
            {
                return;
            }

            var userManager = serviceProvider.GetRequiredService <UserManager <ApplicationUser> >();

            string email    = "*****@*****.**";
            string roleName = "Administrator";
            string password = "******";

            var user = new ApplicationUser
            {
                UserName = email,
                Email    = email,
            };

            await userManager.CreateAsync(user, password);

            await userManager.AddToRoleAsync(user, roleName);

            await dbContext.SaveChangesAsync();
        }
        public async Task CreateSale(SaleInputModel sale, string distributor)
        {
            var distributorId = await db.Distributors
                                .Where(n => n.Name == distributor)
                                .Select(i => i.Id)
                                .FirstOrDefaultAsync();

            if (sale.PharmacyId != 0 &&
                sale.ProductId != 0 &&
                sale.Date != null &&
                sale.Count != 0 &&
                distributorId != 0)
            {
                var saleDbModel = new Sale
                {
                    PharmacyId    = sale.PharmacyId,
                    ProductId     = sale.ProductId,
                    Date          = sale.Date,
                    Count         = sale.Count,
                    DistributorId = distributorId
                };

                await db.Sales.AddAsync(saleDbModel);

                await db.SaveChangesAsync();
            }
        }
Пример #3
0
        public async Task SeedAsync(SpravkiDbContext 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(ApplicationDbContextSeeder));

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

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

                await dbContext.SaveChangesAsync();

                logger.LogInformation($"Seeder {seeder.GetType().Name} done.");
            }
        }