コード例 #1
0
        static void Seed()
        {
            using (var context = new EnrollmentContext())
            {
                if (!context.Instructors.Any())
                {
                    var instructors = new List <Instructor>()
                    {
                        new Instructor {
                            Name = "Uncle Bob"
                        },
                        new Instructor {
                            Name = "Martin Fowler"
                        },
                        new Instructor {
                            Name = "Kent Beck"
                        }
                    };

                    context.AddRange(instructors);
                }

                if (!context.Students.Any())
                {
                    var students = new List <Student>()
                    {
                        new Student()
                        {
                            Name = "Taz Uddin"
                        },
                        new Student()
                        {
                            Name = "Batman the Bat Man"
                        },
                        new Student()
                        {
                            Name = "Popey The Sailor Man"
                        },
                        new Student()
                        {
                            Name = "Jack Sparrow"
                        },
                    };
                    context.AddRange(students);
                }

                if (!context.Courses.Any())
                {
                    var courses = new List <Course>()
                    {
                        new Course()
                        {
                            Title = "The future of programming", InstructorId = 1, StartDate = DateTime.UtcNow, EndDate = DateTime.UtcNow.AddDays(30)
                        },
                        new Course()
                        {
                            Title = "Making architecture matter", InstructorId = 2, StartDate = DateTime.UtcNow, EndDate = DateTime.UtcNow.AddDays(30)
                        },
                        new Course()
                        {
                            Title = "Extreme Programming in practice", InstructorId = 3, StartDate = DateTime.UtcNow, EndDate = DateTime.UtcNow.AddDays(30)
                        },
                        new Course()
                        {
                            Title = "Clean Code", InstructorId = 1, StartDate = DateTime.UtcNow, EndDate = DateTime.UtcNow.AddDays(30)
                        },
                    };
                    context.AddRange(courses);
                }
                context.SaveChanges();
            }
        }