public string CreateTeacher(string teacherName, string teacherID, int courseID) { var teacher = new Teacher() { name = teacherName, AUID = teacherID, CourseID = courseID }; using (var transaction = context.Database.BeginTransaction()) { try { context.Add(teacher); context.SaveChanges(); transaction.Commit(); return("Teacher have been added"); } catch (Exception e) { transaction.Rollback(); return("Teacher could not be added. Teacher with this AUID already exists."); } } }
public string CreateExercise(string lecture, int number, string where, bool open, string teacherID, string studentID, int courseID) { var exercise = new Exercise() { Lecture = lecture, Number = number, Help_Where = where, Open = open, TeacherAUID = teacherID, StudentAUID = studentID, CourseID = courseID }; using (var transaction = context.Database.BeginTransaction()) { try { context.Add(exercise); context.SaveChanges(); transaction.Commit(); return("Exercise have been added"); } catch (Exception e) { transaction.Rollback(); return("Exercise could not be added."); } } }
public string CreateCourse(string courseName) { var course = new Course() { Name = courseName }; using (var transaction = context.Database.BeginTransaction()) { try { context.Add(course); context.SaveChanges(); transaction.Commit(); return("Course have been added"); } catch (Exception e) { transaction.Rollback(); return("Course could not be added. This combination of name and ID already exists"); } } }
public TodoController(Assignment2Context context) { _context = context; if (_context.ModelClass.Count() == 0) { // Create a new TodoItem if collection is empty, // which means you can't delete all TodoItems. _context.ModelClass.Add(new ModelClass { Name = "Item1" }); _context.SaveChanges(); } }
public string CreateAssignment(int courseID, string teacherID) { var assignment = new Assignment() { TeacherAUID = teacherID, CourseID = courseID }; using (var transaction = context.Database.BeginTransaction()) { try { context.Add(assignment); context.SaveChanges(); transaction.Commit(); return("Assignment have been added"); } catch (Exception e) { transaction.Rollback(); return("Assignment could not be added."); } } }
public string CreateStudent(string studentName, string studentID) { var student = new Student() { Name = studentName, AUID = studentID }; using (var transaction = context.Database.BeginTransaction()) { try { context.Add(student); context.SaveChanges(); transaction.Commit(); return("Student have been added"); } catch (Exception e) { transaction.Rollback(); return("Student could not be added. Student with this AUID already exists."); } } }
public static void Main(string[] args) { using (var db = new Assignment2Context()) { db.Database.ExecuteSqlCommand("delete from HelpRequests"); db.Database.ExecuteSqlCommand("delete from StudentCourses"); db.Database.ExecuteSqlCommand("delete from HelpRequests"); db.Database.ExecuteSqlCommand("delete from Exercises"); db.Database.ExecuteSqlCommand("delete from Courses"); db.Database.ExecuteSqlCommand("delete from Teachers"); db.Database.ExecuteSqlCommand("delete from Students"); var student = new Student() { Name = "Jokum", AUID = "AU785594" }; db.Add(student); db.SaveChanges(); var student3 = new Student() { Name = "Jordkim", AUID = "AU789456" }; db.Add(student3); db.SaveChanges(); var student2 = new Student() { Name = "DJDudeMan", AUID = "AU223684" }; db.Add(student2); db.SaveChanges(); var course = new Course() { Name = "Avanceret Løgspark" }; db.Add(course); var course2 = new Course() { Name = "Matematik" }; db.Add(course2); db.SaveChanges(); db.Add(new StudentCourse() //Add a student to a course { Active = true, Course = course, Semester = 2, Student = student }); db.Add(new StudentCourse() //Add another student to the same course { Active = true, Course = course, Student = student2 }); db.SaveChanges(); Teacher teacher2 = new Teacher() { name = "Karl Jorgen", AUID = "AU555555", CourseID = course2.CourseID }; Teacher teacher1 = new Teacher() { name = "Dr. John", AUID = "AU000000", CourseID = course.CourseID }; db.SaveChanges(); Exercise E1 = new Exercise() { Number = 0, Lecture = "English", Help_Where = "Auditoriet", Student = student, Teacher = teacher1, Course = course, Open = true }; db.Add(E1); Exercise E2 = new Exercise() { Number = 420, Lecture = "Danks", Help_Where = "PBA", Student = student, Teacher = teacher1, Course = course, Open = true }; db.Add(E2); Exercise E3 = new Exercise() { Number = 545, Lecture = "Danks", Help_Where = "PBA", Student = student2, Teacher = teacher2, Course = course2 }; db.Add(E3); db.SaveChanges(); var A1 = new Assignment() { Course = course, Teacher = teacher1 }; db.Add(A1); db.SaveChanges(); var A1Students = new HelpRequest() { Open = false, Assignment = A1, Student = student }; db.Add(A1Students); var A1Students2 = new HelpRequest() { Open = true, Assignment = A1, Student = student2 }; db.Add(A1Students2); db.SaveChanges(); } CreateHostBuilder(args).Build().Run(); }