public void Execute() { using (var dbContext = new ApplicationDbContext()) { if (!dbContext.Roles.Any()) { SeedApplicaionRole(dbContext); } if (!dbContext.Users.Any()) { _applicationUser = SeedApplicationUser(dbContext); } if (!dbContext.QuestionCategories.Any()) { SeedQuestionCategory(dbContext); } if (!dbContext.Institutions.Any()) { SeedInstitution(dbContext); } dbContext.SaveChanges(); } }
private static User SeedApplicationUser(DbContext dbContext) { const string email = "*****@*****.**"; const string password = "******"; var userManager = new ApplicationUserManager(new UserStore<User>(dbContext)); userManager.UserValidator = new UserValidator<User>(userManager) { AllowOnlyAlphanumericUserNames = false, RequireUniqueEmail = true }; userManager.PasswordValidator = new PasswordValidator { RequiredLength = 6, RequireNonLetterOrDigit = true, RequireDigit = true, RequireLowercase = true, RequireUppercase = true, }; var user = new User { Email = email, UserName = email, FirstName = "John", LastName = "Doe" }; var result = userManager.Create(user, password); if (!result.Succeeded) return null; userManager.AddToRole(user.Id, "Admin"); return user; }