public CourseModel Create(Course course) { return new CourseModel() { Id = course.Id, Url = _urlHelper.Link("courses",new {id= course.Id}), Name = course.Name, Duration = course.Duration, Description = course.Description, Tutor = Create(course.CourseTutor), Subject = Create(course.CourseSubject) }; }
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 = ((Enums.Gender)Enum.Parse(typeof(Enums.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 = ((Enums.Gender)Enum.Parse(typeof(Enums.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; } }
public Enrollment() { Student = new Student(); Course = new Course(); }
public Course Parse(CourseModel model) { try { var course = new Course() { Name = model.Name, Description = model.Description, Duration = model.Duration, CourseSubject = _repo.GetSubject(model.Subject.Id), CourseTutor = _repo.GetTutor(model.Tutor.Id) }; return course; } catch (Exception) { return null; } }
public bool Update(Course originalCourse, Course updatedCourse) { _ctx.Entry(originalCourse).CurrentValues.SetValues(updatedCourse); //To update child entites in Course entity originalCourse.CourseSubject = updatedCourse.CourseSubject; originalCourse.CourseTutor = updatedCourse.CourseTutor; return true; }
public bool Insert(Course course) { try { _ctx.Courses.Add(course); return true; } catch { return false; } }