Example #1
0
        public static void Seed(SchoolContext context)
        {
            var students = new List <Student>
            {
                new Student {
                    FirstName = "Carson", LastName = "Alexander", EnrollmentDate = DateTime.Parse("2005-09-01")
                },
                new Student {
                    FirstName = "Meredith", LastName = "Alonso", EnrollmentDate = DateTime.Parse("2002-09-01")
                },
                new Student {
                    FirstName = "Arturo", LastName = "Anand", EnrollmentDate = DateTime.Parse("2003-09-01")
                },
                new Student {
                    FirstName = "Gytis", LastName = "Barzdukas", EnrollmentDate = DateTime.Parse("2002-09-01")
                },
                new Student {
                    FirstName = "Yan", LastName = "Li", EnrollmentDate = DateTime.Parse("2002-09-01")
                },
                new Student {
                    FirstName = "Peggy", LastName = "Justice", EnrollmentDate = DateTime.Parse("2001-09-01")
                },
                new Student {
                    FirstName = "Laura", LastName = "Norman", EnrollmentDate = DateTime.Parse("2003-09-01")
                },
                new Student {
                    FirstName = "Nino", LastName = "Olivetto", EnrollmentDate = DateTime.Parse("2005-09-01")
                }
            };

            students.ForEach(s => context.Students.Add(s));
            context.SaveChanges();

            var courses = new List <Course>
            {
                new Course {
                    CourseCode = 1050, Title = "Chemistry"
                },
                new Course {
                    CourseCode = 4022, Title = "Microeconomics"
                },
                new Course {
                    CourseCode = 4041, Title = "Macroeconomics"
                },
                new Course {
                    CourseCode = 1045, Title = "Calculus"
                },
                new Course {
                    CourseCode = 3141, Title = "Trigonometry"
                },
                new Course {
                    CourseCode = 2021, Title = "Composition"
                },
                new Course {
                    CourseCode = 2042, Title = "Literature"
                }
            };

            courses.ForEach(s => context.Courses.Add(s));
            context.SaveChanges();

            var enrollments = new List <Enrollment>
            {
                new Enrollment {
                    StudentID = 1, CourseCode = 1050, Grade = GradesEnum.A
                },
                new Enrollment {
                    StudentID = 1, CourseCode = 4022, Grade = GradesEnum.C
                },
                new Enrollment {
                    StudentID = 1, CourseCode = 4041, Grade = GradesEnum.B
                },
                new Enrollment {
                    StudentID = 2, CourseCode = 1045, Grade = GradesEnum.B
                },
                new Enrollment {
                    StudentID = 2, CourseCode = 3141, Grade = GradesEnum.F
                },
                new Enrollment {
                    StudentID = 2, CourseCode = 2021, Grade = GradesEnum.F
                },
                new Enrollment {
                    StudentID = 3, CourseCode = 1050
                },
                new Enrollment {
                    StudentID = 4, CourseCode = 1050
                },
                new Enrollment {
                    StudentID = 4, CourseCode = 4022, Grade = GradesEnum.F
                },
                new Enrollment {
                    StudentID = 5, CourseCode = 4041, Grade = GradesEnum.C
                },
                new Enrollment {
                    StudentID = 6, CourseCode = 1045
                },
                new Enrollment {
                    StudentID = 7, CourseCode = 3141, Grade = GradesEnum.A
                }
            };

            enrollments.ForEach(s => context.Enrollments.Add(s));
            context.SaveChanges();
        }
Example #2
0
        public static void Initialize(SchoolContext context)
        {
            //EnsureCreated method will create Database,which will drop old db then re-create it.
            //we can also use Code First Migrations to change DB schema rather then using above way
            context.Database.EnsureCreated();

            // Look for any students.
            if (context.Students.Any())
            {
                return;   // DB has been seeded
            }

            var students = new Models.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")
                }
            };

            foreach (Student s in students)
            {
                context.Students.Add(s);
            }
            context.SaveChanges();

            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
                }
            };

            foreach (Course c in courses)
            {
                context.Courses.Add(c);
            }
            context.SaveChanges();

            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
                },
            };

            foreach (Enrollment e in enrollments)
            {
                context.Enrollments.Add(e);
            }
            context.SaveChanges();
        }
 public void Save()
 {
     db.SaveChanges();
 }