public async Task InitializeData() { ////////// Create Two Roles (Admin, User) and one admin account && one user account assigned with proper roles ////////// var findAdminRole = await _roleManager.FindByNameAsync("Admin"); var findUserRole = await _roleManager.FindByNameAsync("User"); var adminRole = new IdentityRole("Admin"); var userRole = new IdentityRole("User"); //If admin role does not exists, create it if (findAdminRole == null) { await _roleManager.CreateAsync(adminRole); } //If user role does not exists, create it if (findUserRole == null) { await _roleManager.CreateAsync(userRole); } var findAdminAccount = await _userManager.FindByNameAsync("*****@*****.**"); //If there is no user account "*****@*****.**", create it if (findAdminAccount == null) { var admin = new ApplicationUser() { UserName = "******", Email = "*****@*****.**", FirstName = "She Jong", LastName = "Shon", SecurityStamp = Guid.NewGuid().ToString() }; var result = await _userManager.CreateAsync(admin, "P@$$w0rd"); var account = await _userManager.FindByEmailAsync(admin.Email); account.EmailConfirmed = true; try { if (result.Succeeded) { await _context.SaveChangesAsync(); } } catch (Exception ex) { throw ex; } } var adminAccount = await _userManager.FindByNameAsync("*****@*****.**"); //If Admin account is not in an admin role, add it to the role. if (!await _userManager.IsInRoleAsync(adminAccount, adminRole.Name)) { await _userManager.AddToRoleAsync(adminAccount, adminRole.Name); } var findUserAccount = await _userManager.FindByNameAsync("*****@*****.**"); //If there is no user account "[email protected], create it if (findUserAccount == null) { var user = new ApplicationUser() { UserName = "******", Email = "*****@*****.**", FirstName = "Mike", LastName = "Carson", SecurityStamp = Guid.NewGuid().ToString() }; var result = await _userManager.CreateAsync(user, "P@$$w0rd"); var account = await _userManager.FindByEmailAsync(user.Email); account.EmailConfirmed = true; try { if (result.Succeeded) { await _context.SaveChangesAsync(); } } catch (Exception ex) { throw ex; } } var userAccount = await _userManager.FindByNameAsync("*****@*****.**"); //If User account is not in an User role, add it to the role. if (!await _userManager.IsInRoleAsync(userAccount, userRole.Name)) { await _userManager.AddToRoleAsync(userAccount, userRole.Name); } var findUserAccount2 = await _userManager.FindByNameAsync("*****@*****.**"); //If there is no user account "[email protected], create it if (findUserAccount2 == null) { var user = new ApplicationUser() { UserName = "******", Email = "*****@*****.**", FirstName = "Jennifer", LastName = "Louse", SecurityStamp = Guid.NewGuid().ToString() }; var result = await _userManager.CreateAsync(user, "P@$$w0rd"); var account = await _userManager.FindByEmailAsync(user.Email); account.EmailConfirmed = true; try { if (result.Succeeded) { await _context.SaveChangesAsync(); } } catch (Exception ex) { throw ex; } } var userAccount2 = await _userManager.FindByNameAsync("*****@*****.**"); //If User account is not in an User role, add it to the role. if (!await _userManager.IsInRoleAsync(userAccount2, userRole.Name)) { await _userManager.AddToRoleAsync(userAccount2, userRole.Name); } var findUserAccount3 = await _userManager.FindByNameAsync("*****@*****.**"); //If there is no user account "[email protected], create it if (findUserAccount3 == null) { var user = new ApplicationUser() { UserName = "******", Email = "*****@*****.**", FirstName = "Peter", LastName = "Hanson", SecurityStamp = Guid.NewGuid().ToString() }; var result = await _userManager.CreateAsync(user, "P@$$w0rd"); var account = await _userManager.FindByEmailAsync(user.Email); account.EmailConfirmed = true; try { if (result.Succeeded) { await _context.SaveChangesAsync(); } } catch (Exception ex) { throw ex; } } var userAccount3 = await _userManager.FindByNameAsync("*****@*****.**"); //If User account is not in an User role, add it to the role. if (!await _userManager.IsInRoleAsync(userAccount3, userRole.Name)) { await _userManager.AddToRoleAsync(userAccount3, userRole.Name); } ///////////////////////////////////////////////////////////////////////////////////////////////////// // Look for any students. if (_context.Students.Any()) { return; // DB has been seeded } var students = new Student[] { new Student { FirstMidName = "Carson", LastName = "Alexander", EnrollmentDate = DateTime.Parse("2010-09-01") }, new Student { FirstMidName = "Meredith", LastName = "Alonso", EnrollmentDate = DateTime.Parse("2012-09-01") }, new Student { FirstMidName = "Arturo", LastName = "Anand", EnrollmentDate = DateTime.Parse("2013-09-01") }, new Student { FirstMidName = "Gytis", LastName = "Barzdukas", EnrollmentDate = DateTime.Parse("2012-09-01") }, new Student { FirstMidName = "Yan", LastName = "Li", EnrollmentDate = DateTime.Parse("2012-09-01") }, new Student { FirstMidName = "Peggy", LastName = "Justice", EnrollmentDate = DateTime.Parse("2011-09-01") }, new Student { FirstMidName = "Laura", LastName = "Norman", EnrollmentDate = DateTime.Parse("2013-09-01") }, new Student { FirstMidName = "Nino", LastName = "Olivetto", EnrollmentDate = DateTime.Parse("2005-09-01") } }; foreach (Student s in students) { _context.Students.Add(s); } var instructors = new Instructor[] { new Instructor { FirstMidName = "Kim", LastName = "Abercrombie", HireDate = DateTime.Parse("1995-03-11") }, new Instructor { FirstMidName = "Fadi", LastName = "Fakhouri", HireDate = DateTime.Parse("2002-07-06") }, new Instructor { FirstMidName = "Roger", LastName = "Harui", HireDate = DateTime.Parse("1998-07-01") }, new Instructor { FirstMidName = "Candace", LastName = "Kapoor", HireDate = DateTime.Parse("2001-01-15") }, new Instructor { FirstMidName = "Roger", LastName = "Zheng", HireDate = DateTime.Parse("2004-02-12") } }; foreach (Instructor i in instructors) { _context.Instructors.Add(i); } var departments = new Department[] { new Department { Name = "English", Budget = 350000, StartDate = DateTime.Parse("2007-09-01"), InstructorID = instructors.Single(i => i.LastName == "Abercrombie").ID }, new Department { Name = "Mathematics", Budget = 100000, StartDate = DateTime.Parse("2007-09-01"), InstructorID = instructors.Single(i => i.LastName == "Fakhouri").ID }, new Department { Name = "Engineering", Budget = 350000, StartDate = DateTime.Parse("2007-09-01"), InstructorID = instructors.Single(i => i.LastName == "Harui").ID }, new Department { Name = "Economics", Budget = 100000, StartDate = DateTime.Parse("2007-09-01"), InstructorID = instructors.Single(i => i.LastName == "Kapoor").ID } }; foreach (Department d in departments) { _context.Departments.Add(d); } var courses = new Course[] { new Course { CourseID = 1050, Title = "Chemistry", Credits = 3, DepartmentID = departments.Single(s => s.Name == "Engineering").DepartmentID }, new Course { CourseID = 4022, Title = "Microeconomics", Credits = 3, DepartmentID = departments.Single(s => s.Name == "Economics").DepartmentID }, new Course { CourseID = 4041, Title = "Macroeconomics", Credits = 3, DepartmentID = departments.Single(s => s.Name == "Economics").DepartmentID }, new Course { CourseID = 1045, Title = "Calculus", Credits = 4, DepartmentID = departments.Single(s => s.Name == "Mathematics").DepartmentID }, new Course { CourseID = 3141, Title = "Trigonometry", Credits = 4, DepartmentID = departments.Single(s => s.Name == "Mathematics").DepartmentID }, new Course { CourseID = 2021, Title = "Composition", Credits = 3, DepartmentID = departments.Single(s => s.Name == "English").DepartmentID }, new Course { CourseID = 2042, Title = "Literature", Credits = 4, DepartmentID = departments.Single(s => s.Name == "English").DepartmentID }, }; foreach (Course c in courses) { _context.Courses.Add(c); } var officeAssignments = new OfficeAssignment[] { new OfficeAssignment { InstructorID = instructors.Single(i => i.LastName == "Fakhouri").ID, Location = "Smith 17" }, new OfficeAssignment { InstructorID = instructors.Single(i => i.LastName == "Harui").ID, Location = "Gowan 27" }, new OfficeAssignment { InstructorID = instructors.Single(i => i.LastName == "Kapoor").ID, Location = "Thompson 304" }, }; foreach (OfficeAssignment o in officeAssignments) { _context.OfficeAssignments.Add(o); } var courseInstructors = new CourseAssignment[] { new CourseAssignment { CourseID = courses.Single(c => c.Title == "Chemistry").CourseID, InstructorID = instructors.Single(i => i.LastName == "Kapoor").ID }, new CourseAssignment { CourseID = courses.Single(c => c.Title == "Chemistry").CourseID, InstructorID = instructors.Single(i => i.LastName == "Harui").ID }, new CourseAssignment { CourseID = courses.Single(c => c.Title == "Microeconomics").CourseID, InstructorID = instructors.Single(i => i.LastName == "Zheng").ID }, new CourseAssignment { CourseID = courses.Single(c => c.Title == "Macroeconomics").CourseID, InstructorID = instructors.Single(i => i.LastName == "Zheng").ID }, new CourseAssignment { CourseID = courses.Single(c => c.Title == "Calculus").CourseID, InstructorID = instructors.Single(i => i.LastName == "Fakhouri").ID }, new CourseAssignment { CourseID = courses.Single(c => c.Title == "Trigonometry").CourseID, InstructorID = instructors.Single(i => i.LastName == "Harui").ID }, new CourseAssignment { CourseID = courses.Single(c => c.Title == "Composition").CourseID, InstructorID = instructors.Single(i => i.LastName == "Abercrombie").ID }, new CourseAssignment { CourseID = courses.Single(c => c.Title == "Literature").CourseID, InstructorID = instructors.Single(i => i.LastName == "Abercrombie").ID }, }; foreach (CourseAssignment ci in courseInstructors) { _context.CourseAssignments.Add(ci); } var enrollments = new Enrollment[] { new Enrollment { StudentID = students.Single(s => s.LastName == "Alexander").ID, CourseID = courses.Single(c => c.Title == "Chemistry").CourseID, Grade = Grade.A }, new Enrollment { StudentID = students.Single(s => s.LastName == "Alexander").ID, CourseID = courses.Single(c => c.Title == "Microeconomics").CourseID, Grade = Grade.C }, new Enrollment { StudentID = students.Single(s => s.LastName == "Alexander").ID, CourseID = courses.Single(c => c.Title == "Macroeconomics").CourseID, Grade = Grade.B }, new Enrollment { StudentID = students.Single(s => s.LastName == "Alonso").ID, CourseID = courses.Single(c => c.Title == "Calculus").CourseID, Grade = Grade.B }, new Enrollment { StudentID = students.Single(s => s.LastName == "Alonso").ID, CourseID = courses.Single(c => c.Title == "Trigonometry").CourseID, Grade = Grade.B }, new Enrollment { StudentID = students.Single(s => s.LastName == "Alonso").ID, CourseID = courses.Single(c => c.Title == "Composition").CourseID, Grade = Grade.B }, new Enrollment { StudentID = students.Single(s => s.LastName == "Anand").ID, CourseID = courses.Single(c => c.Title == "Chemistry").CourseID, Grade = Grade.F }, new Enrollment { StudentID = students.Single(s => s.LastName == "Anand").ID, CourseID = courses.Single(c => c.Title == "Microeconomics").CourseID, Grade = Grade.B }, new Enrollment { StudentID = students.Single(s => s.LastName == "Barzdukas").ID, CourseID = courses.Single(c => c.Title == "Chemistry").CourseID, Grade = Grade.B }, new Enrollment { StudentID = students.Single(s => s.LastName == "Li").ID, CourseID = courses.Single(c => c.Title == "Composition").CourseID, Grade = Grade.B }, new Enrollment { StudentID = students.Single(s => s.LastName == "Justice").ID, CourseID = courses.Single(c => c.Title == "Literature").CourseID, Grade = Grade.B } }; foreach (Enrollment e in enrollments) { var enrollmentInDataBase = _context.Enrollments.Where( s => s.Student.ID == e.StudentID && s.Course.CourseID == e.CourseID).SingleOrDefault(); if (enrollmentInDataBase == null) { _context.Enrollments.Add(e); } } await _context.SaveChangesAsync(); }
public async Task InitializeData() { var findAdminRole = await _roleManager.FindByNameAsync("Admin"); var findUserRole = await _roleManager.FindByNameAsync("User"); var adminRole = new IdentityRole("Admin"); var userRole = new IdentityRole("User"); if (findAdminRole == null) { await _roleManager.CreateAsync(adminRole); } if (findUserRole == null) { await _roleManager.CreateAsync(userRole); } await CreateUserAndRole(adminRole, "*****@*****.**", "Admin", "Guy"); await CreateUserAndRole(userRole, "*****@*****.**", "Mike", "Carson"); await CreateUserAndRole(userRole, "*****@*****.**", "Jennifer", "Louise"); await CreateUserAndRole(userRole, "*****@*****.**", "Peter", "Hanson"); if (_context.Students.Any()) { return; } var students = new Student[] { new Student { FirstName = "Carson", LastName = "Alexander", EnrollmentDate = DateTime.Parse("2010-09-01") }, new Student { FirstName = "Meredith", LastName = "Alonso", EnrollmentDate = DateTime.Parse("2012-09-01") }, new Student { FirstName = "Arturo", LastName = "Anand", EnrollmentDate = DateTime.Parse("2013-09-01") }, new Student { FirstName = "Gytis", LastName = "Barzdukas", EnrollmentDate = DateTime.Parse("2012-09-01") }, new Student { FirstName = "Yan", LastName = "Li", EnrollmentDate = DateTime.Parse("2012-09-01") }, new Student { FirstName = "Peggy", LastName = "Justice", EnrollmentDate = DateTime.Parse("2011-09-01") }, new Student { FirstName = "Laura", LastName = "Norman", EnrollmentDate = DateTime.Parse("2013-09-01") }, new Student { FirstName = "Nino", LastName = "Olivetto", EnrollmentDate = DateTime.Parse("2005-09-01") } }; foreach (Student s in students) { _context.Students.Add(s); } var instructors = new Instructor[] { new Instructor { FirstName = "Kim", LastName = "Abercrombie", HireDate = DateTime.Parse("1995-03-11") }, new Instructor { FirstName = "Fadi", LastName = "Fakhouri", HireDate = DateTime.Parse("2002-07-06") }, new Instructor { FirstName = "Roger", LastName = "Harui", HireDate = DateTime.Parse("1998-07-01") }, new Instructor { FirstName = "Candace", LastName = "Kapoor", HireDate = DateTime.Parse("2001-01-15") }, new Instructor { FirstName = "Roger", LastName = "Zheng", HireDate = DateTime.Parse("2004-02-12") } }; foreach (var i in instructors) { _context.Instructors.Add(i); } var departments = new Department[] { new Department { Name = "English", Budget = 350000, StartDate = DateTime.Parse("2007-09-01"), InstructorID = instructors.Single(i => i.LastName == "Abercrombie").InstructorID }, new Department { Name = "Mathematics", Budget = 100000, StartDate = DateTime.Parse("2007-09-01"), InstructorID = instructors.Single(i => i.LastName == "Fakhouri").InstructorID }, new Department { Name = "Engineering", Budget = 350000, StartDate = DateTime.Parse("2007-09-01"), InstructorID = instructors.Single(i => i.LastName == "Harui").InstructorID }, new Department { Name = "Economics", Budget = 100000, StartDate = DateTime.Parse("2007-09-01"), InstructorID = instructors.Single(i => i.LastName == "Kapoor").InstructorID } }; foreach (var d in departments) { _context.Departments.Add(d); } var courses = new Course[] { new Course { CourseID = 1050, Title = "Chemistry", Credits = 3, DepartmentID = departments.Single(s => s.Name == "Engineering").DepartmentID }, new Course { CourseID = 4022, Title = "Microeconomics", Credits = 3, DepartmentID = departments.Single(s => s.Name == "Economics").DepartmentID }, new Course { CourseID = 4041, Title = "Macroeconomics", Credits = 3, DepartmentID = departments.Single(s => s.Name == "Economics").DepartmentID }, new Course { CourseID = 1045, Title = "Calculus", Credits = 4, DepartmentID = departments.Single(s => s.Name == "Mathematics").DepartmentID }, new Course { CourseID = 3141, Title = "Trigonometry", Credits = 4, DepartmentID = departments.Single(s => s.Name == "Mathematics").DepartmentID }, new Course { CourseID = 2021, Title = "Composition", Credits = 3, DepartmentID = departments.Single(s => s.Name == "English").DepartmentID }, new Course { CourseID = 2042, Title = "Literature", Credits = 4, DepartmentID = departments.Single(s => s.Name == "English").DepartmentID }, }; foreach (var c in courses) { _context.Courses.Add(c); } var officeAssignments = new OfficeAssignment[] { new OfficeAssignment { InstructorID = instructors.Single(i => i.LastName == "Fakhouri").InstructorID, Location = "Smith 17" }, new OfficeAssignment { InstructorID = instructors.Single(i => i.LastName == "Harui").InstructorID, Location = "Gowan 27" }, new OfficeAssignment { InstructorID = instructors.Single(i => i.LastName == "Kapoor").InstructorID, Location = "Thompson 304" }, }; foreach (var o in officeAssignments) { _context.OfficeAssignments.Add(o); } var courseInstructors = new CourseAssignment[] { new CourseAssignment { CourseID = courses.Single(c => c.Title == "Chemistry").CourseID, InstructorID = instructors.Single(i => i.LastName == "Kapoor").InstructorID }, new CourseAssignment { CourseID = courses.Single(c => c.Title == "Chemistry").CourseID, InstructorID = instructors.Single(i => i.LastName == "Harui").InstructorID }, new CourseAssignment { CourseID = courses.Single(c => c.Title == "Microeconomics").CourseID, InstructorID = instructors.Single(i => i.LastName == "Zheng").InstructorID }, new CourseAssignment { CourseID = courses.Single(c => c.Title == "Macroeconomics").CourseID, InstructorID = instructors.Single(i => i.LastName == "Zheng").InstructorID }, new CourseAssignment { CourseID = courses.Single(c => c.Title == "Calculus").CourseID, InstructorID = instructors.Single(i => i.LastName == "Fakhouri").InstructorID }, new CourseAssignment { CourseID = courses.Single(c => c.Title == "Trigonometry").CourseID, InstructorID = instructors.Single(i => i.LastName == "Harui").InstructorID }, new CourseAssignment { CourseID = courses.Single(c => c.Title == "Composition").CourseID, InstructorID = instructors.Single(i => i.LastName == "Abercrombie").InstructorID }, new CourseAssignment { CourseID = courses.Single(c => c.Title == "Literature").CourseID, InstructorID = instructors.Single(i => i.LastName == "Abercrombie").InstructorID }, }; foreach (var ci in courseInstructors) { _context.CourseAssignments.Add(ci); } var enrollments = new Enrollment[] { new Enrollment { StudentID = students.Single(s => s.LastName == "Alexander").StudentID, CourseID = courses.Single(c => c.Title == "Chemistry").CourseID, Grade = Grade.A }, new Enrollment { StudentID = students.Single(s => s.LastName == "Alexander").StudentID, CourseID = courses.Single(c => c.Title == "Microeconomics").CourseID, Grade = Grade.C }, new Enrollment { StudentID = students.Single(s => s.LastName == "Alexander").StudentID, CourseID = courses.Single(c => c.Title == "Macroeconomics").CourseID, Grade = Grade.B }, new Enrollment { StudentID = students.Single(s => s.LastName == "Alonso").StudentID, CourseID = courses.Single(c => c.Title == "Calculus").CourseID, Grade = Grade.B }, new Enrollment { StudentID = students.Single(s => s.LastName == "Alonso").StudentID, CourseID = courses.Single(c => c.Title == "Trigonometry").CourseID, Grade = Grade.B }, new Enrollment { StudentID = students.Single(s => s.LastName == "Alonso").StudentID, CourseID = courses.Single(c => c.Title == "Composition").CourseID, Grade = Grade.B }, new Enrollment { StudentID = students.Single(s => s.LastName == "Anand").StudentID, CourseID = courses.Single(c => c.Title == "Chemistry").CourseID, Grade = Grade.F }, new Enrollment { StudentID = students.Single(s => s.LastName == "Anand").StudentID, CourseID = courses.Single(c => c.Title == "Microeconomics").CourseID, Grade = Grade.B }, new Enrollment { StudentID = students.Single(s => s.LastName == "Barzdukas").StudentID, CourseID = courses.Single(c => c.Title == "Chemistry").CourseID, Grade = Grade.B }, new Enrollment { StudentID = students.Single(s => s.LastName == "Li").StudentID, CourseID = courses.Single(c => c.Title == "Composition").CourseID, Grade = Grade.B }, new Enrollment { StudentID = students.Single(s => s.LastName == "Justice").StudentID, CourseID = courses.Single(c => c.Title == "Literature").CourseID, Grade = Grade.B } }; foreach (Enrollment e in enrollments) { var enrollmentInDatabase = _context.Enrollments.Where( s => s.StudentID == e.StudentID && s.Course.CourseID == e.CourseID).SingleOrDefault(); if (enrollmentInDatabase == null) { _context.Enrollments.Add(e); } } await _context.SaveChangesAsync(); }