public static void ImportCourses() { var db = new StudentsDbContext(); Console.Write("Adding Homeworks"); for (int i = 0; i < DefaultAddingNumber; i++) { var courseToAdd = new Course() { Name = RandomGenerator.GetRandomString(5, 20), Description = RandomGenerator.GetRandomString(5, 20) }; db.Courses.Add(courseToAdd); db.SaveChanges(); } db.SaveChanges(); }
public static void ImportHomeworks() { var db = new StudentsDbContext();; Console.Write("Adding Homeworks"); var courseIds = db.Courses.Select(c => c.Id).ToList(); var studentIds = db.Students.Select(s => s.Id).ToList(); for (int i = 0; i < DefaultAddingNumber; i++) { var homeworkToAdd = new Homework() { Content = RandomGenerator.GetRandomString(1, 100), TimeSent = null, CourseId = courseIds[RandomGenerator.GetRandomNumber(0, courseIds.Count - 1)], StudentId = studentIds[RandomGenerator.GetRandomNumber(0, studentIds.Count - 1)] }; db.Homeworks.Add(homeworkToAdd); } db.SaveChanges(); }
public static void ImportStudents() { var db = new StudentsDbContext(); Console.Write("Adding Homeworks"); var courseIds = db.Courses.Select(c => c.Id).ToList(); for (int i = 0; i < DefaultAddingNumber; i++) { var studentToAdd = new Student() { FirstName = RandomGenerator.GetRandomString(5, 15), LastName = RandomGenerator.GetRandomString(5, 15), Age = RandomGenerator.GetRandomNumber(20, 30) }; if (courseIds.Count > 0) { var uniqueCourseIds = new HashSet<int>(); var numberOfCoursesPerStudent = RandomGenerator.GetRandomNumber(1, Math.Min(10, courseIds.Count)); while (uniqueCourseIds.Count != numberOfCoursesPerStudent) { uniqueCourseIds.Add(courseIds[RandomGenerator.GetRandomNumber(0, courseIds.Count - 1)]); } foreach (var uniqueCourseId in uniqueCourseIds) { var course = db.Courses.Find(uniqueCourseId); studentToAdd.Courses.Add(course); } } db.Students.Add(studentToAdd); } db.SaveChanges(); }