public ComplementoDeRespuesta InsertOrRemoveCourse(StudentPerCourse model) { var rh = new ComplementoDeRespuesta(); try { using (var ctx = _dbContextScopeFactory.Create()) { var courseByStudent = _studentPerCourseRepository.Find(x => x.StudentId == model.StudentId && x.CourseId == model.CourseId ).SingleOrDefault(); if (courseByStudent == null) { model.SuscribedAt = DateTime.Now; _studentPerCourseRepository.Insert(model); } else { _studentPerCourseRepository.Delete(courseByStudent); } ctx.SaveChanges(); rh.SetRespuesta(true); } } catch (Exception e) { logger.Error(e.Message); } return(rh); }
public ResponseHelper Insert(StudentPerCourse model) { var rh = new ResponseHelper(); try { using (var ctx = _dbContextScopeFactory.Create()) { _studentPerCourseRepository.Insert(model); ctx.SaveChanges(); rh.SetResponse(true); } } catch (Exception e) { logger.Error(e.Message); } return(rh); }
//EXECUTE COMMANDS public static void InsertData(int choice) { if (commands.ContainsKey(choice)) { var connectionString = ConfigurationManager.ConnectionStrings["PrivateSchoolConnectionString"].ConnectionString; using (SqlConnection connection = new SqlConnection(connectionString)) { //Match user's choice with command using (SqlCommand command = new SqlCommand(commands[choice], connection)) { bool addMore = false; do { switch (choice) { case 10: Student.Create(command); break; case 11: Trainer.Create(command); break; case 12: Course.Create(command); break; case 13: Assignment.Create(command); break; case 14: StudentPerCourse.Create(command); break; case 15: TrainerPerCourse.Create(command); break; case 16: AssignmentPerStudent.Create(command); break; } try { connection.Open(); command.ExecuteNonQuery(); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { connection.Close(); } Console.WriteLine("\n\nDo you want to add more? Enter Y for yes.\nOtherwise, press any other key to return to the menu."); var answer = Console.ReadLine(); if (answer.ToUpper() == "Y") { addMore = true; } else { addMore = false; } }while (addMore); } } } }