Exemplo n.º 1
0
        private static void AddHomeworks(int homeworksCount)
        {
            var dbContext = new StudentsSystemDbContext();

            var coursesIds = dbContext.Courses
                                    .Select(c => c.Id)
                                    .ToList();

            int coursesIdsCount = coursesIds.Count;

            var studentsIds = dbContext.Students
                                    .Select(st => st.Id)
                                    .ToList();

            for (int i = 0; i < homeworksCount; i++)
            {
                var hw = new Homework();
                hw.Content = RandomDataGenerator.GetString(1, 500);

                int courseIndex = RandomDataGenerator.GetInteger(0, coursesIdsCount - 1);
                hw.CourseId = coursesIds[courseIndex];

                // works while students count is equal or greater than homeworks count
                hw.StudentId = studentsIds[i];

                dbContext.Homeworks.Add(hw);

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

            dbContext.SaveChanges();
        }
Exemplo n.º 2
0
        private static void AddStudents(int studentsCount)
        {
            var dbContext = new StudentsSystemDbContext();

            for (int i = 0; i < studentsCount; i++)
            {
                var student = new Student();
                student.FirstName = RandomDataGenerator.GetString(2, 10);
                student.LastName = RandomDataGenerator.GetString(2, 15);
                student.FacultyNumber = RandomDataGenerator.GetString(10, 10);

                dbContext.Students.Add(student);

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

            dbContext.SaveChanges();
        }