Example #1
0
 public Course()
 {
     Enrollments = new List<Enrollment>();
     CourseTutor = new Tutor();
     CourseSubject = new Subject();
 }
        public void Seed()
        {
            if (_ctx.Courses.Count() > 0)
            {
                return;
            }

            try
            {
                foreach (var subjectName in subjects)
                {
                    var subject = new Subject
                    {
                        Name = subjectName
                    };
                    _ctx.Subjects.Add(subject);
                    _ctx.SaveChanges();
                }

                for (int i = 0; i < tutorNames.Length; i++)
                {
                    var nameGenderMail = SplitValue(tutorNames[i]);
                    var tutor = new Tutor
                    {
                        Email = String.Format("{0}.{1}@{2}", nameGenderMail[0], nameGenderMail[1], nameGenderMail[3]),
                        UserName = String.Format("{0}{1}", nameGenderMail[0], nameGenderMail[1]),
                        Password = RandomString(8),
                        FirstName = nameGenderMail[0],
                        LastName = nameGenderMail[1],
                        Gender = ((Gender)Enum.Parse(typeof(Gender), nameGenderMail[2]))
                    };

                    _ctx.Tutors.Add(tutor);

                    var courseSubject = _ctx.Subjects.Where(s => s.Id == i + 1).Single();

                    foreach (var CourseDataItem in CoursesSeedData.Where(c => c.SubjectID == courseSubject.Id))
                    {
                        var course = new Course
                        {
                            Name = CourseDataItem.CourseName,
                            CourseSubject = courseSubject,
                            CourseTutor = tutor,
                            Duration = new Random().Next(3, 6),
                            Description = String.Format("The course will talk in depth about: {0}", CourseDataItem.CourseName)
                        };
                        _ctx.Courses.Add(course);
                    }
                }

                _ctx.SaveChanges();

                for (int i = 0; i < studentNames.Length; i++)
                {
                    var nameGenderMail = SplitValue(studentNames[i]);
                    var student = new Student()
                    {
                        Email = String.Format("{0}.{1}@{2}", nameGenderMail[0], nameGenderMail[1], nameGenderMail[3]),
                        UserName = String.Format("{0}{1}", nameGenderMail[0], nameGenderMail[1]),
                        Password = RandomString(8),
                        FirstName = nameGenderMail[0],
                        LastName = nameGenderMail[1],
                        Gender = ((Gender)Enum.Parse(typeof(Gender), nameGenderMail[2])),
                        DateOfBirth = DateTime.UtcNow.AddDays(-new Random().Next(7000, 8000)),
                        RegistrationDate = DateTime.UtcNow.AddDays(-new Random().Next(365, 730))
                    };

                    _ctx.Students.Add(student);

                    int maxCoursesId = _ctx.Courses.Max(c => c.Id);

                    //To enroll in 4 courses randomly
                    for (int z = 0; z < 4; z++)
                    {

                        int randomCourseId = new Random().Next(1, maxCoursesId);

                        var enrollment = new Enrollment
                        {
                            Student = student,
                            Course = _ctx.Courses.Where(c => c.Id == randomCourseId).Single(),
                            EnrollmentDate = DateTime.UtcNow.AddDays(-new Random().Next(10, 30))
                        };
                        _ctx.Enrollments.Add(enrollment);
                    }
                }

                _ctx.SaveChanges();
            }
            catch (Exception ex)
            {
                string message = ex.ToString();
                throw ex;
            }
        }