コード例 #1
0
        public static void Initialize(IServiceProvider serviceProvider)
        {
            using (var context = new UserDbContext(serviceProvider.GetRequiredService <DbContextOptions <UserDbContext> >()))
            {
                if (context.Users.Any())
                {
                    return;
                }

                string defaultPassword = "******";

                byte[] passwordHash, passwordSalt;
                UtilityService.CreatePasswordHash(defaultPassword, out passwordHash, out passwordSalt);

                context.Users.AddRange(
                    new User
                {
                    Id           = 1,
                    FirstName    = "Normal",
                    LastName     = "User",
                    Username     = "******",
                    Role         = "Regular",
                    PasswordHash = passwordHash,
                    PasswordSalt = passwordSalt,
                    Discount     = new Discount
                    {
                        Id         = 1,
                        UserID     = 1,
                        Percentage = 0
                    }
                },
                    new User
                {
                    Id           = 2,
                    FirstName    = "Privileged",
                    LastName     = "User",
                    Username     = "******",
                    Role         = "Privileged",
                    PasswordHash = passwordHash,
                    PasswordSalt = passwordSalt,
                    Discount     = new Discount
                    {
                        Id         = 2,
                        UserID     = 2,
                        Percentage = 2
                    }
                });

                context.SaveChanges();
            }
        }