(MVC) MVC (2018)

<<назад DbInitializer.vb (Contoso University).

   1:  Namespace Data
   2:   
   3:      Public Class DbInitializer
   4:   
   5:          Public Shared Sub Initialize(ByVal context As SchoolContext)
   6:              If context.Students.Any() Then
   7:                  Return
   8:              End If
   9:   
  10:              Dim students = New Models.Student() {
  11:                  New Models.Student With {.FirstMidName = "Carson", .LastName = "Alexander", .EnrollmentDate = DateTime.Parse("2010-09-01")},
  12:                  New Models.Student With {.FirstMidName = "Meredith", .LastName = "Alonso", .EnrollmentDate = DateTime.Parse("2012-09-01")},
  13:                  New Models.Student With {.FirstMidName = "Arturo", .LastName = "Anand", .EnrollmentDate = DateTime.Parse("2013-09-01")},
  14:                  New Models.Student With {.FirstMidName = "Gytis", .LastName = "Barzdukas", .EnrollmentDate = DateTime.Parse("2012-09-01")},
  15:                  New Models.Student With {.FirstMidName = "Yan", .LastName = "Li", .EnrollmentDate = DateTime.Parse("2012-09-01")},
  16:                  New Models.Student With {.FirstMidName = "Peggy", .LastName = "Justice", .EnrollmentDate = DateTime.Parse("2011-09-01")},
  17:                  New Models.Student With {.FirstMidName = "Laura", .LastName = "Norman", .EnrollmentDate = DateTime.Parse("2013-09-01")},
  18:                  New Models.Student With {.FirstMidName = "Nino", .LastName = "Olivetto", .EnrollmentDate = DateTime.Parse("2005-09-01")}}
  19:              For Each s As Models.Student In students
  20:                  context.Students.Add(s)
  21:              Next
  22:              context.SaveChanges()
  23:   
  24:              Dim instructors = New Models.Instructor() {
  25:                  New Models.Instructor With {.FirstMidName = "Kim", .LastName = "Abercrombie", .HireDate = DateTime.Parse("1995-03-11")},
  26:                  New Models.Instructor With {.FirstMidName = "Fadi", .LastName = "Fakhouri", .HireDate = DateTime.Parse("2002-07-06")},
  27:                  New Models.Instructor With {.FirstMidName = "Roger", .LastName = "Harui", .HireDate = DateTime.Parse("1998-07-01")},
  28:                  New Models.Instructor With {.FirstMidName = "Candace", .LastName = "Kapoor", .HireDate = DateTime.Parse("2001-01-15")},
  29:                  New Models.Instructor With {.FirstMidName = "Roger", .LastName = "Zheng", .HireDate = DateTime.Parse("2004-02-12")}}
  30:              For Each i As Models.Instructor In instructors
  31:                  context.Instructors.Add(i)
  32:              Next
  33:              context.SaveChanges()
  34:   
  35:              Dim departments = New Models.Department() {
  36:                  New Models.Department With {.Name = "English", .Budget = 350000, .StartDate = DateTime.Parse("2007-09-01"), .InstructorID = instructors.Single(Function(i) i.LastName = "Abercrombie").ID},
  37:                  New Models.Department With {.Name = "Mathematics", .Budget = 100000, .StartDate = DateTime.Parse("2007-09-01"), .InstructorID = instructors.Single(Function(i) i.LastName = "Fakhouri").ID},
  38:                  New Models.Department With {.Name = "Engineering", .Budget = 350000, .StartDate = DateTime.Parse("2007-09-01"), .InstructorID = instructors.Single(Function(i) i.LastName = "Harui").ID},
  39:                  New Models.Department With {.Name = "Economics", .Budget = 100000, .StartDate = DateTime.Parse("2007-09-01"), .InstructorID = instructors.Single(Function(i) i.LastName = "Kapoor").ID}}
  40:              For Each d As Models.Department In departments
  41:                  context.Departments.Add(d)
  42:              Next
  43:              context.SaveChanges()
  44:   
  45:              Dim courses = New Models.Course() {
  46:                  New Models.Course With {.CourseID = 1050, .Title = "Chemistry", .Credits = 3, .DepartmentID = departments.Single(Function(s) s.Name = "Engineering").DepartmentID},
  47:                  New Models.Course With {.CourseID = 4022, .Title = "Microeconomics", .Credits = 3, .DepartmentID = departments.Single(Function(s) s.Name = "Economics").DepartmentID},
  48:                  New Models.Course With {.CourseID = 4041, .Title = "Macroeconomics", .Credits = 3, .DepartmentID = departments.Single(Function(s) s.Name = "Economics").DepartmentID},
  49:                  New Models.Course With {.CourseID = 1045, .Title = "Calculus", .Credits = 4, .DepartmentID = departments.Single(Function(s) s.Name = "Mathematics").DepartmentID},
  50:                  New Models.Course With {.CourseID = 3141, .Title = "Trigonometry", .Credits = 4, .DepartmentID = departments.Single(Function(s) s.Name = "Mathematics").DepartmentID},
  51:                  New Models.Course With {.CourseID = 2021, .Title = "Composition", .Credits = 3, .DepartmentID = departments.Single(Function(s) s.Name = "English").DepartmentID},
  52:                  New Models.Course With {.CourseID = 2042, .Title = "Literature", .Credits = 4, .DepartmentID = departments.Single(Function(s) s.Name = "English").DepartmentID}}
  53:              For Each c As Models.Course In courses
  54:                  context.Courses.Add(c)
  55:              Next
  56:              context.SaveChanges()
  57:   
  58:              Dim officeAssignments = New Models.OfficeAssignment() {
  59:                  New Models.OfficeAssignment With {.InstructorID = instructors.Single(Function(i) i.LastName = "Fakhouri").ID, .Location = "Smith 17"},
  60:                  New Models.OfficeAssignment With {.InstructorID = instructors.Single(Function(i) i.LastName = "Harui").ID, .Location = "Gowan 27"},
  61:                  New Models.OfficeAssignment With {.InstructorID = instructors.Single(Function(i) i.LastName = "Kapoor").ID, .Location = "Thompson 304"}}
  62:              For Each o As Models.OfficeAssignment In officeAssignments
  63:                  context.OfficeAssignments.Add(o)
  64:              Next
  65:              context.SaveChanges()
  66:   
  67:              Dim courseInstructors = New Models.CourseAssignment() {
  68:                  New Models.CourseAssignment With {.CourseID = courses.Single(Function(c) c.Title = "Chemistry").CourseID, .InstructorID = instructors.Single(Function(i) i.LastName = "Kapoor").ID},
  69:                  New Models.CourseAssignment With {.CourseID = courses.Single(Function(c) c.Title = "Chemistry").CourseID, .InstructorID = instructors.Single(Function(i) i.LastName = "Harui").ID},
  70:                  New Models.CourseAssignment With {.CourseID = courses.Single(Function(c) c.Title = "Microeconomics").CourseID, .InstructorID = instructors.Single(Function(i) i.LastName = "Zheng").ID},
  71:                  New Models.CourseAssignment With {.CourseID = courses.Single(Function(c) c.Title = "Macroeconomics").CourseID, .InstructorID = instructors.Single(Function(i) i.LastName = "Zheng").ID},
  72:                  New Models.CourseAssignment With {.CourseID = courses.Single(Function(c) c.Title = "Calculus").CourseID, .InstructorID = instructors.Single(Function(i) i.LastName = "Fakhouri").ID},
  73:                  New Models.CourseAssignment With {.CourseID = courses.Single(Function(c) c.Title = "Trigonometry").CourseID, .InstructorID = instructors.Single(Function(i) i.LastName = "Harui").ID},
  74:                  New Models.CourseAssignment With {.CourseID = courses.Single(Function(c) c.Title = "Composition").CourseID, .InstructorID = instructors.Single(Function(i) i.LastName = "Abercrombie").ID},
  75:                  New Models.CourseAssignment With {.CourseID = courses.Single(Function(c) c.Title = "Literature").CourseID, .InstructorID = instructors.Single(Function(i) i.LastName = "Abercrombie").ID}}
  76:              For Each ci As Models.CourseAssignment In courseInstructors
  77:                  context.CourseAssignments.Add(ci)
  78:              Next
  79:              context.SaveChanges()
  80:   
  81:              Dim enrollments = New Models.Enrollment() {
  82:                  New Models.Enrollment With {.StudentID = students.Single(Function(s) s.LastName = "Alexander").ID, .CourseID = courses.Single(Function(c) c.Title = "Chemistry").CourseID, .Grade = Models.Grade.A},
  83:                  New Models.Enrollment With {.StudentID = students.Single(Function(s) s.LastName = "Alexander").ID, .CourseID = courses.Single(Function(c) c.Title = "Microeconomics").CourseID, .Grade = Models.Grade.C},
  84:                  New Models.Enrollment With {.StudentID = students.Single(Function(s) s.LastName = "Alexander").ID, .CourseID = courses.Single(Function(c) c.Title = "Macroeconomics").CourseID, .Grade = Models.Grade.B},
  85:                  New Models.Enrollment With {.StudentID = students.Single(Function(s) s.LastName = "Alonso").ID, .CourseID = courses.Single(Function(c) c.Title = "Calculus").CourseID, .Grade = Models.Grade.B},
  86:                  New Models.Enrollment With {.StudentID = students.Single(Function(s) s.LastName = "Alonso").ID, .CourseID = courses.Single(Function(c) c.Title = "Trigonometry").CourseID, .Grade = Models.Grade.B},
  87:                  New Models.Enrollment With {.StudentID = students.Single(Function(s) s.LastName = "Alonso").ID, .CourseID = courses.Single(Function(c) c.Title = "Composition").CourseID, .Grade = Models.Grade.B},
  88:                  New Models.Enrollment With {.StudentID = students.Single(Function(s) s.LastName = "Anand").ID, .CourseID = courses.Single(Function(c) c.Title = "Chemistry").CourseID},
  89:                  New Models.Enrollment With {.StudentID = students.Single(Function(s) s.LastName = "Anand").ID, .CourseID = courses.Single(Function(c) c.Title = "Microeconomics").CourseID, .Grade = Models.Grade.B},
  90:                  New Models.Enrollment With {.StudentID = students.Single(Function(s) s.LastName = "Barzdukas").ID, .CourseID = courses.Single(Function(c) c.Title = "Chemistry").CourseID, .Grade = Models.Grade.B},
  91:                  New Models.Enrollment With {.StudentID = students.Single(Function(s) s.LastName = "Li").ID, .CourseID = courses.Single(Function(c) c.Title = "Composition").CourseID, .Grade = Models.Grade.B},
  92:                  New Models.Enrollment With {.StudentID = students.Single(Function(s) s.LastName = "Justice").ID, .CourseID = courses.Single(Function(c) c.Title = "Literature").CourseID, .Grade = Models.Grade.B}}
  93:              For Each e As Models.Enrollment In enrollments
  94:                  Dim enrollmentInDataBase = context.Enrollments.SingleOrDefault(Function(s) s.Student.ID = e.StudentID AndAlso s.Course.CourseID = e.CourseID)
  95:                  If enrollmentInDataBase Is Nothing Then
  96:                      context.Enrollments.Add(e)
  97:                  End If
  98:              Next
  99:              context.SaveChanges()
 100:   
 101:          End Sub
 102:      End Class
 103:  End Namespace


Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19>  <20>  <21>  <22>  <23
Link to this page: //www.vb-net.com/EF-missing-FAQ/Code/DbInitializer.vb.htm
<SITEMAP>  <MVC>  <ASP>  <NET>  <DATA>  <KIOSK>  <FLEX>  <SQL>  <NOTES>  <LINUX>  <MONO>  <FREEWARE>  <DOCS>  <ENG>  <CHAT ME>  <ABOUT ME>  < THANKS ME>