Пример #1
0
        /// <summary>
        /// 数据库无数据时,初始化一些数据给数据库
        /// </summary>
        /// <param name="context">数据库上下文</param>
        public static void Initialize(SchoolContext context)
        {
            // 方法用于自动创建数据库
            context.Database.EnsureCreated();

            // 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("2005-09-01")
                },
                new Student {
                    FirstMidName = "Meredith", LastName = "Alonso", EnrollmentDate = DateTime.Parse("2002-09-01")
                },
                new Student {
                    FirstMidName = "Arturo", LastName = "Anand", EnrollmentDate = DateTime.Parse("2003-09-01")
                },
                new Student {
                    FirstMidName = "Gytis", LastName = "Barzdukas", EnrollmentDate = DateTime.Parse("2002-09-01")
                },
                new Student {
                    FirstMidName = "Yan", LastName = "Li", EnrollmentDate = DateTime.Parse("2002-09-01")
                },
                new Student {
                    FirstMidName = "Peggy", LastName = "Justice", EnrollmentDate = DateTime.Parse("2001-09-01")
                },
                new Student {
                    FirstMidName = "Laura", LastName = "Norman", EnrollmentDate = DateTime.Parse("2003-09-01")
                },
                new Student {
                    FirstMidName = "Nino", LastName = "Olivetto", EnrollmentDate = DateTime.Parse("2005-09-01")
                }
            };

            var courses = new Course[]
            {
                new Course {
                    CourseID = 1050, Title = "Chemistry", Credits = 3
                },
                new Course {
                    CourseID = 4022, Title = "Microeconomics", Credits = 3
                },
                new Course {
                    CourseID = 4041, Title = "Macroeconomics", Credits = 3
                },
                new Course {
                    CourseID = 1045, Title = "Calculus", Credits = 4
                },
                new Course {
                    CourseID = 3141, Title = "Trigonometry", Credits = 4
                },
                new Course {
                    CourseID = 2021, Title = "Composition", Credits = 3
                },
                new Course {
                    CourseID = 2042, Title = "Literature", Credits = 4
                }
            };

            var enrollments = new Enrollment[]
            {
                new Enrollment {
                    StudentID = 1, CourseID = 1050, Grade = Grade.A
                },
                new Enrollment {
                    StudentID = 1, CourseID = 4022, Grade = Grade.C
                },
                new Enrollment {
                    StudentID = 1, CourseID = 4041, Grade = Grade.B
                },
                new Enrollment {
                    StudentID = 2, CourseID = 1045, Grade = Grade.B
                },
                new Enrollment {
                    StudentID = 2, CourseID = 3141, Grade = Grade.F
                },
                new Enrollment {
                    StudentID = 2, CourseID = 2021, Grade = Grade.F
                },
                new Enrollment {
                    StudentID = 3, CourseID = 1050
                },
                new Enrollment {
                    StudentID = 4, CourseID = 1050
                },
                new Enrollment {
                    StudentID = 4, CourseID = 4022, Grade = Grade.F
                },
                new Enrollment {
                    StudentID = 5, CourseID = 4041, Grade = Grade.C
                },
                new Enrollment {
                    StudentID = 6, CourseID = 1045
                },
                new Enrollment {
                    StudentID = 7, CourseID = 3141, Grade = Grade.A
                },
            };

            context.AddRange(courses);
            context.AddRange(students);
            context.AddRange(enrollments);
            context.SaveChanges();
        }
Пример #2
0
        public static void Initialize(SchoolContext context)
        {
            // Look for any students.
            if (context.Students.Any())
            {
                return;   // DB has been seeded
            }

            var alexander = new Student
            {
                FirstMidName   = "Carson",
                LastName       = "Alexander",
                EnrollmentDate = DateTime.Parse("2016-09-01")
            };

            var alonso = new Student
            {
                FirstMidName   = "Meredith",
                LastName       = "Alonso",
                EnrollmentDate = DateTime.Parse("2018-09-01")
            };

            var anand = new Student
            {
                FirstMidName   = "Arturo",
                LastName       = "Anand",
                EnrollmentDate = DateTime.Parse("2019-09-01")
            };

            var barzdukas = new Student
            {
                FirstMidName   = "Gytis",
                LastName       = "Barzdukas",
                EnrollmentDate = DateTime.Parse("2018-09-01")
            };

            var li = new Student
            {
                FirstMidName   = "Yan",
                LastName       = "Li",
                EnrollmentDate = DateTime.Parse("2018-09-01")
            };

            var justice = new Student
            {
                FirstMidName   = "Peggy",
                LastName       = "Justice",
                EnrollmentDate = DateTime.Parse("2017-09-01")
            };

            var norman = new Student
            {
                FirstMidName   = "Laura",
                LastName       = "Norman",
                EnrollmentDate = DateTime.Parse("2019-09-01")
            };

            var olivetto = new Student
            {
                FirstMidName   = "Nino",
                LastName       = "Olivetto",
                EnrollmentDate = DateTime.Parse("2011-09-01")
            };

            var abercrombie = new Instructor
            {
                FirstMidName = "Kim",
                LastName     = "Abercrombie",
                HireDate     = DateTime.Parse("1995-03-11")
            };

            var fakhouri = new Instructor
            {
                FirstMidName = "Fadi",
                LastName     = "Fakhouri",
                HireDate     = DateTime.Parse("2002-07-06")
            };

            var harui = new Instructor
            {
                FirstMidName = "Roger",
                LastName     = "Harui",
                HireDate     = DateTime.Parse("1998-07-01")
            };

            var kapoor = new Instructor
            {
                FirstMidName = "Candace",
                LastName     = "Kapoor",
                HireDate     = DateTime.Parse("2001-01-15")
            };

            var zheng = new Instructor
            {
                FirstMidName = "Roger",
                LastName     = "Zheng",
                HireDate     = DateTime.Parse("2004-02-12")
            };

            var officeAssignments = new OfficeAssignment[]
            {
                new OfficeAssignment {
                    Instructor = fakhouri,
                    Location   = "Smith 17"
                },
                new OfficeAssignment {
                    Instructor = harui,
                    Location   = "Gowan 27"
                },
                new OfficeAssignment {
                    Instructor = kapoor,
                    Location   = "Thompson 304"
                },
            };

            context.AddRange(officeAssignments);

            var english = new Department
            {
                Name          = "English",
                Budget        = 350000,
                StartDate     = DateTime.Parse("2007-09-01"),
                Administrator = abercrombie
            };

            var mathematics = new Department
            {
                Name          = "Mathematics",
                Budget        = 100000,
                StartDate     = DateTime.Parse("2007-09-01"),
                Administrator = fakhouri
            };

            var engineering = new Department
            {
                Name          = "Engineering",
                Budget        = 350000,
                StartDate     = DateTime.Parse("2007-09-01"),
                Administrator = harui
            };

            var economics = new Department
            {
                Name          = "Economics",
                Budget        = 100000,
                StartDate     = DateTime.Parse("2007-09-01"),
                Administrator = kapoor
            };

            var chemistry = new Course
            {
                CourseID    = 1050,
                Title       = "Chemistry",
                Credits     = 3,
                Department  = engineering,
                Instructors = new List <Instructor> {
                    kapoor, harui
                }
            };

            var microeconomics = new Course
            {
                CourseID    = 4022,
                Title       = "Microeconomics",
                Credits     = 3,
                Department  = economics,
                Instructors = new List <Instructor> {
                    zheng
                }
            };

            var macroeconmics = new Course
            {
                CourseID    = 4041,
                Title       = "Macroeconomics",
                Credits     = 3,
                Department  = economics,
                Instructors = new List <Instructor> {
                    zheng
                }
            };

            var calculus = new Course
            {
                CourseID    = 1045,
                Title       = "Calculus",
                Credits     = 4,
                Department  = mathematics,
                Instructors = new List <Instructor> {
                    fakhouri
                }
            };

            var trigonometry = new Course
            {
                CourseID    = 3141,
                Title       = "Trigonometry",
                Credits     = 4,
                Department  = mathematics,
                Instructors = new List <Instructor> {
                    harui
                }
            };

            var composition = new Course
            {
                CourseID    = 2021,
                Title       = "Composition",
                Credits     = 3,
                Department  = english,
                Instructors = new List <Instructor> {
                    abercrombie
                }
            };

            var literature = new Course
            {
                CourseID    = 2042,
                Title       = "Literature",
                Credits     = 4,
                Department  = english,
                Instructors = new List <Instructor> {
                    abercrombie
                }
            };

            var enrollments = new Enrollment[]
            {
                new Enrollment {
                    Student = alexander,
                    Course  = chemistry,
                    Grade   = Grade.A
                },
                new Enrollment {
                    Student = alexander,
                    Course  = microeconomics,
                    Grade   = Grade.C
                },
                new Enrollment {
                    Student = alexander,
                    Course  = macroeconmics,
                    Grade   = Grade.B
                },
                new Enrollment {
                    Student = alonso,
                    Course  = calculus,
                    Grade   = Grade.B
                },
                new Enrollment {
                    Student = alonso,
                    Course  = trigonometry,
                    Grade   = Grade.B
                },
                new Enrollment {
                    Student = alonso,
                    Course  = composition,
                    Grade   = Grade.B
                },
                new Enrollment {
                    Student = anand,
                    Course  = chemistry,
                },
                new Enrollment {
                    Student = anand,
                    Course  = microeconomics,
                    Grade   = Grade.B
                },
                new Enrollment {
                    Student = barzdukas,
                    Course  = chemistry,
                    Grade   = Grade.B
                },
                new Enrollment {
                    Student = li,
                    Course  = composition,
                    Grade   = Grade.B
                },
                new Enrollment {
                    Student = justice,
                    Course  = literature,
                    Grade   = Grade.B
                }
            };

            context.AddRange(enrollments);
            context.SaveChanges();
        }
Пример #3
0
        public static void Initialize(SchoolContext context)
        {
            // Look for any students
            if (context.Students.Any())
            {
                Console.WriteLine("Found one");
                // DB has data or has been seeded
                return;
            }
            Console.WriteLine("None found");

            // students
            var studentPM = new Student {
                FirstMidName = "Peter", LastName = "Mayaga", EnrollmentDate = DateTime.Parse("2015-05-03")
            };
            var studentJK = new Student {
                FirstMidName = "John", LastName = "Kampulunguse", EnrollmentDate = DateTime.Parse("2018-12-30")
            };
            var studentWS = new Student {
                FirstMidName = "Were", LastName = "Simon", EnrollmentDate = DateTime.Parse("2020-10-21")
            };
            var studentCJ = new Student {
                FirstMidName = "Collins", LastName = "Jackson", EnrollmentDate = DateTime.Parse("2009-01-02")
            };
            var studentMS = new Student {
                FirstMidName = "Matthew", LastName = "Sekitoleko", EnrollmentDate = DateTime.Parse("2021-3-23")
            };

            // instructors
            var instructor1 = new Instructor {
                FirstMidName = "John", LastName = "Ung", HireDate = DateTime.Parse("2001-04-14")
            };
            var instructor2 = new Instructor {
                FirstMidName = "Kabenge", LastName = "Moses", HireDate = DateTime.Parse("2005-01-29")
            };
            var instructor3 = new Instructor {
                FirstMidName = "Ssenoga", LastName = "Abram", HireDate = DateTime.Parse("2001-11-02")
            };
            var instructor4 = new Instructor {
                FirstMidName = "Wandegeya", LastName = "Kulekana", HireDate = DateTime.Parse("2015-10-30")
            };

            // OfficeAssignments
            var officeAssignments = new OfficeAssignment[]
            {
                new OfficeAssignment {
                    Instructor = instructor1, Location = "LLT-1A"
                },
                new OfficeAssignment {
                    Instructor = instructor3, Location = "LLT-2B"
                },
                new OfficeAssignment {
                    Instructor = instructor4, Location = "Big lab A"
                }
            };

            context.AddRange(officeAssignments);

            // departments
            var english = new Department
            {
                Name          = "English",
                Budget        = 300000,
                StartDate     = DateTime.Parse("1990-02-15"),
                Administrator = instructor1
            };
            var mathematics = new Department
            {
                Name          = "Mathematics",
                Budget        = 250000,
                StartDate     = DateTime.Parse("1960-10-25"),
                Administrator = instructor2
            };
            var cs = new Department
            {
                Name          = "Computer Science",
                Budget        = 500000,
                StartDate     = DateTime.Parse("1990-09-22"),
                Administrator = instructor3
            };

            // courses
            var chemistry = new Course
            {
                CourseID    = 1050,
                Title       = "Chemistry",
                Credits     = 3,
                Department  = mathematics,
                Instructors = new List <Instructor> {
                    instructor1, instructor2
                }
            };
            var SE = new Course
            {
                CourseID    = 5060,
                Title       = "Software Engineering",
                Credits     = 4,
                Department  = cs,
                Instructors = new List <Instructor> {
                    instructor2, instructor3
                }
            };
            var discreteMaths = new Course
            {
                CourseID    = 5090,
                Title       = "Discrete Mathematics",
                Credits     = 5,
                Department  = cs,
                Instructors = new List <Instructor> {
                    instructor2, instructor3, instructor4
                }
            };

            // Enrollments
            var enrollments = new Enrollment[]
            {
                new Enrollment
                {
                    Student = studentCJ,
                    Course  = chemistry,
                    Grade   = Grade.D
                },
                new Enrollment
                {
                    Student = studentJK,
                    Course  = discreteMaths
                },
                new Enrollment
                {
                    Student = studentPM,
                    Course  = SE,
                    Grade   = Grade.A
                },
                new Enrollment
                {
                    Student = studentJK,
                    Course  = SE,
                    Grade   = Grade.B
                },
                new Enrollment
                {
                    Student = studentMS,
                    Course  = chemistry
                }
                // studentWS not used
            };

            context.AddRange(enrollments);

            // Save changes
            context.SaveChanges();
        }