Ejemplo n.º 1
0
        public static void Main()
        {
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<AcademyDbContext, Configuration>());

            var db = new AcademyDbContext();

            using (db)
            {
                Console.Write("Courses count: ");
                Console.WriteLine(db.Courses.Count());
                Console.Write("Students count: ");
                Console.WriteLine(db.Students.Count());
            }
        }
Ejemplo n.º 2
0
        private static void GenerateRandomAcademyData(AcademyDbContext db, int numberOfCourses, int numberOfStudents)
        {
            // Generate random courses
            for (int i = 0; i < numberOfCourses; i++)
            {
                db.Courses.Add(GetRandomCourse());
                Console.WriteLine("Course added.");
            }

            db.SaveChanges();

            // Generate random students
            for (int i = 0; i < numberOfStudents; i++)
            {
                db.Students.Add(GetRandomStudent(1000));
                Console.WriteLine("Student added.");
            }

            db.SaveChanges();

            // Add about 20 students for each course +-5 students
            var courses = db.Courses;
            var allStudents = db.Students
                .ToList();

            foreach (var course in courses)
            {
                var numberOfStudentsForCurrentCourse = randomGenerator.GetRandomInt(15, 25);
                var studentIdsForCurrentCourse = allStudents
                    .OrderBy(s => Guid.NewGuid())
                    .Take(numberOfStudentsForCurrentCourse);

                foreach (var student in studentIdsForCurrentCourse)
                {
                    course.Students.Add(student);
                    Console.WriteLine("Added student {0} to course {1}.", student.StudentId, course.CourseId);
                }
            }

            db.SaveChanges();

            // Add Homerok to 90% of students in the courses
            var courseIdsStudentIds = db.Courses
                .Select(cs => new
                {
                    CourseId = cs.CourseId,
                    StudentIds = cs.Students.Select(s => s.StudentId)
                })
                .ToList();

            foreach (var group in courseIdsStudentIds)
            {
                var courseId = group.CourseId;
                var selectedStudentIds = group.StudentIds
                    .OrderBy(s => Guid.NewGuid())
                    .Take(group.StudentIds.Count() * 90 / 100)
                    .ToList();

                foreach (var studentId in selectedStudentIds)
                {
                    db.Homeworks.Add(GetRandomHomework(courseId, studentId));
                    Console.WriteLine("Added homework.");
                }
            }

            db.SaveChanges();
        }
Ejemplo n.º 3
0
        // Tasks 2 and 3
        static void Main()
        {
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<AcademyDbContext, Configuration>());

            var db = new AcademyDbContext();

            db.Students.Count();

            GenerateRandomAcademyData(db, 15, 50);
        }