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();
        }
Beispiel #2
0
        public static void Main()
        {
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<StudentsSystemDbContext, Configuration>());

            var db = new StudentsSystemDbContext();

            db.Courses.Count();
        }
        public void Seed(int count)
        {
            Console.Write("Importing homeworks:");

            var db = new StudentsSystemDbContext();
            var allStudentIds = db
                .Students
                .Select(s => s.Id)
                .ToList();
            for (int i = 0; i < count; i++)
            {
                var studentId = RandomGenerator.GetRandomNumber(0, allStudentIds.Count - 1);
                var homework = new Homework
                {
                    Content = RandomGenerator.GetRandomString(50, 150),
                    TimeSent = RandomGenerator.GetRandomDate(before: DateTime.Now),
                    StudentId = allStudentIds[studentId]
                };

                db.Homeworks.Add(homework);

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

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

            db.SaveChanges();
            Console.WriteLine();
        }
        public void Seed(int count)
        {
            Console.Write("Importing courses:");

            var db = new StudentsSystemDbContext();
            var allStudents = db
                .Students
                .OrderBy(s => Guid.NewGuid())
                .ToList();
            var allHomeworks = db
                .Homeworks
                .OrderBy(h => Guid.NewGuid())
                .ToList();

            for (int i = 0; i < count; i++)
            {
                var currentStudentInCourse = RandomGenerator.GetRandomNumber(10, 30);
                var currentStartIndex = RandomGenerator.GetRandomNumber(0,
                    allStudents.Count - currentStudentInCourse - 1);
                var studentsList = new HashSet<Student>();
                for (int j = 0; j < currentStudentInCourse; j++)
                {
                    studentsList.Add(allStudents[currentStartIndex]);
                    currentStartIndex++;
                }

                var currentHomeworsInCourse = RandomGenerator.GetRandomNumber(5, 15);
                currentStartIndex = RandomGenerator.GetRandomNumber(0,
                    allHomeworks.Count - currentHomeworsInCourse - 1);
                var homeworksList = new HashSet<Homework>();
                for (int j = 0; j < currentHomeworsInCourse; j++)
                {
                    homeworksList.Add(allHomeworks[currentStartIndex]);
                    currentStartIndex++;
                }

                var course = new Course
                {
                    Name = RandomGenerator.GetRandomString(10, 100),
                    Description = RandomGenerator.GetRandomString(50, 500),
                    Students = studentsList,
                    Homeworks = homeworksList
                };

                db.Courses.Add(course);

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

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

            db.SaveChanges();
            Console.WriteLine();
        }