Exemplo n.º 1
0
        public void Seed(int count)
        {
            Console.Write("Importing students:");

            var db = new StudentsSystemDbContext();
            for (int i = 0; i < count; i++)
            {
                var student = new Student
                {
                    Name = RandomGenerator.GetRandomString(10, 50),
                    Number = RandomGenerator.GetRandomString(10, 10)
                };

                db.Students.Add(student);

                if (i % 10 == 0)
                {
                    Console.Write(".");
                }

                if (i % 100 == 0)
                {
                    db.SaveChanges();
                    db.Dispose();
                    db = new StudentsSystemDbContext();
                }
            }

            db.SaveChanges();
            Console.WriteLine();
        }
Exemplo n.º 2
0
        static void Main()
        {
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<StudentsSystemContext, Configuration>());
            
            using (StudentsSystemContext dbContext = new StudentsSystemContext())
            {
                var firstHomework = new Homework()
                {
                    Content = "Simple database1",
                    TimeSent = DateTime.Now,
                };

                var secondHomework = new Homework()
                {
                    Content = "Simple database2",
                    TimeSent = DateTime.Now,
                };

                var student = new Student()
                {
                    FirstName = "Nikolay",
                    LastName = "Kostadinov",
                    Number = "965552",
                };

                var course = new Course()
                {
                    Name = "DataBase",
                    Description = "Entity Framework Code First",
                    Materials = "Lections, homework"
                };

                student.Homeworks.Add(firstHomework);
                student.Homeworks.Add(secondHomework);
                student.Courses.Add(course);
                course.Homeworks.Add(secondHomework);
                course.Students.Add(student);

                dbContext.Students.Add(student);
                dbContext.Courses.Add(course);
                dbContext.Homeworks.Add(firstHomework);
                dbContext.Homeworks.Add(secondHomework);

                dbContext.SaveChanges();
            }
        }