public static void Seed( JPPQUIZ21.Data.ApplicationDbContext dbContext, RoleManager <IdentityRole> roleManager, UserManager <JPPQUIZ21.Data.Models.ApplicationUser> userManager ) { // Create default Users (if there are none) if (!dbContext.Users.Any()) { CreateUsers(dbContext, roleManager, userManager) .GetAwaiter() .GetResult(); } // Create default Quizzes (if there are none) together with their set of Q&A if (!dbContext.Quizzes.Any()) { CreateQuizzes(dbContext); } }
private static async Task CreateUsers( JPPQUIZ21.Data.ApplicationDbContext dbContext, RoleManager <IdentityRole> roleManager, UserManager <JPPQUIZ21.Data.Models.ApplicationUser> userManager) { // local variables DateTime createdDate = new DateTime(2016, 03, 01, 12, 30, 00); DateTime lastModifiedDate = DateTime.Now; string role_Administrator = "Administrator"; string role_RegisteredUser = "******"; //Create Roles (if they doesn't exist yet) if (!await roleManager.RoleExistsAsync(role_Administrator)) { await roleManager.CreateAsync(new IdentityRole(role_Administrator)); } if (!await roleManager.RoleExistsAsync(role_RegisteredUser)) { await roleManager.CreateAsync(new IdentityRole(role_RegisteredUser)); } // Create the "Admin" ApplicationUser account var user_Admin = new JPPQUIZ21.Data.Models.ApplicationUser() { SecurityStamp = Guid.NewGuid().ToString(), UserName = "******", Email = "*****@*****.**", CreatedDate = createdDate, LastModifiedDate = lastModifiedDate }; // Insert "Admin" into the Database and assign the "Administrator" and "Registered" roles to him. if (await userManager.FindByNameAsync(user_Admin.UserName) == null) { await userManager.CreateAsync(user_Admin, "Pass4Admin"); await userManager.AddToRoleAsync(user_Admin, role_RegisteredUser); await userManager.AddToRoleAsync(user_Admin, role_Administrator); // Remove Lockout and E-Mail confirmation. user_Admin.EmailConfirmed = true; user_Admin.LockoutEnabled = false; } #if DEBUG // Create some sample registered user accounts var user_Ryan = new JPPQUIZ21.Data.Models.ApplicationUser() { SecurityStamp = Guid.NewGuid().ToString(), UserName = "******", Email = "*****@*****.**", CreatedDate = createdDate, LastModifiedDate = lastModifiedDate }; var user_Solice = new JPPQUIZ21.Data.Models.ApplicationUser() { SecurityStamp = Guid.NewGuid().ToString(), UserName = "******", Email = "*****@*****.**", CreatedDate = createdDate, LastModifiedDate = lastModifiedDate }; var user_Vodan = new JPPQUIZ21.Data.Models.ApplicationUser() { SecurityStamp = Guid.NewGuid().ToString(), UserName = "******", Email = "*****@*****.**", CreatedDate = createdDate, LastModifiedDate = lastModifiedDate }; // Insert sample registered users into the Database and also assign the "Registered" role to him. if (await userManager.FindByNameAsync(user_Ryan.UserName) == null) { await userManager.CreateAsync(user_Ryan, "Pass4Ryan"); await userManager.AddToRoleAsync(user_Ryan, role_RegisteredUser); // Remove Lockout and E-Mail confirmation. user_Ryan.EmailConfirmed = true; user_Ryan.LockoutEnabled = false; } if (await userManager.FindByNameAsync(user_Solice.UserName) == null) { await userManager.CreateAsync(user_Solice, "Pass4Solice"); await userManager.AddToRoleAsync(user_Solice, role_RegisteredUser); // Remove Lockout and E-Mail confirmation. user_Solice.EmailConfirmed = true; user_Solice.LockoutEnabled = false; } if (await userManager.FindByNameAsync(user_Vodan.UserName) == null) { await userManager.CreateAsync(user_Vodan, "Pass4Vodan"); await userManager.AddToRoleAsync(user_Vodan, role_RegisteredUser); // Remove Lockout and E-Mail confirmation. user_Vodan.EmailConfirmed = true; user_Vodan.LockoutEnabled = false; } #endif await dbContext.SaveChangesAsync(); }
/// <summary> /// Creates a sample quiz and add it to the Database /// together with a sample set of questions, answers & results. /// </summary> /// <param name="userId">the author ID</param> /// <param name="id">the quiz ID</param> /// <param name="createdDate">the quiz CreatedDate</param> private static void CreateSampleQuiz( JPPQUIZ21.Data.ApplicationDbContext dbContext, int num, string authorId, int viewCount, int numberOfQuestions, int numberOfAnswersPerQuestion, int numberOfResults, DateTime createdDate) { var quiz = new Quiz() { UserId = authorId, Title = String.Format("Quiz {0} Title", num), Description = String.Format("This is a sample description for quiz {0}.", num), Text = "This is a sample quiz created by the DbSeeder class for testing purposes. " + "All the questions, answers & results are auto-generated as well.", ViewCount = viewCount, CreatedDate = createdDate, LastModifiedDate = createdDate }; dbContext.Quizzes.Add(quiz); dbContext.SaveChanges(); for (int i = 0; i < numberOfQuestions; i++) { var question = new Question() { QuizId = quiz.Id, Text = "This is a sample question created by the DbSeeder class for testing purposes. " + "All the child answers are auto-generated as well.", CreatedDate = createdDate, LastModifiedDate = createdDate }; dbContext.Questions.Add(question); dbContext.SaveChanges(); for (int i2 = 0; i2 < numberOfAnswersPerQuestion; i2++) { var e2 = dbContext.Answers.Add(new Answer() { QuestionId = question.Id, Text = "This is a sample answer created by the DbSeeder class for testing purposes. ", Value = i2, CreatedDate = createdDate, LastModifiedDate = createdDate }); } } for (int i = 0; i < numberOfResults; i++) { dbContext.Results.Add(new Result() { QuizId = quiz.Id, Text = "This is a sample result created by the DbSeeder class for testing purposes. ", MinValue = 0, // max value should be equal to answers number * max answer value MaxValue = numberOfAnswersPerQuestion * 2, CreatedDate = createdDate, LastModifiedDate = createdDate }); } dbContext.SaveChanges(); }
private static void CreateQuizzes(JPPQUIZ21.Data.ApplicationDbContext dbContext) { // local variables DateTime createdDate = new DateTime(2017, 08, 08, 12, 30, 00); DateTime lastModifiedDate = DateTime.Now; // retrieve the admin user, which we'll use as default author. var authorId = dbContext.Users .Where(u => u.UserName == "Admin") .FirstOrDefault() .Id; #if DEBUG // create 47 sample quizzes with auto-generated data // (including questions, answers & results) var num = 47; for (int i = 1; i <= num; i++) { CreateSampleQuiz( dbContext, i, authorId, num - i, 3, 3, 3, createdDate.AddDays(-num)); } #endif // create 3 more quizzes with better descriptive data // (we'll add the questions, answers & results later on) EntityEntry <Quiz> e1 = dbContext.Quizzes.Add(new Quiz() { UserId = authorId, Title = "Are you more Light or Dark side of the Force?", Description = "Star Wars personality test", Text = @"Choose wisely you must, young padawan: " + "this test will prove if your will is strong enough " + "to adhere to the principles of the light side of the Force " + "or if you're fated to embrace the dark side. " + "No you want to become a true JEDI, you can't possibly miss this!", ViewCount = 2343, CreatedDate = createdDate, LastModifiedDate = lastModifiedDate }); EntityEntry <Quiz> e2 = dbContext.Quizzes.Add(new Quiz() { UserId = authorId, Title = "GenX, GenY or Genz?", Description = "Find out what decade most represents you", Text = @"Do you feel confortable in your generation? " + "What year should you have been born in?" + "Here's a bunch of questions that will help you to find out!", ViewCount = 4180, CreatedDate = createdDate, LastModifiedDate = lastModifiedDate }); EntityEntry <Quiz> e3 = dbContext.Quizzes.Add(new Quiz() { UserId = authorId, Title = "Which Shingeki No Kyojin character are you?", Description = "Attack On Titan personality test", Text = @"Do you relentlessly seek revenge like Eren? " + "Are you willing to put your like on the stake to protect your friends like Mikasa? " + "Would you trust your fighting skills like Levi " + "or rely on your strategies and tactics like Arwin? " + "Unveil your true self with this Attack On Titan personality test!", ViewCount = 5203, CreatedDate = createdDate, LastModifiedDate = lastModifiedDate }); // persist the changes on the Database dbContext.SaveChanges(); }