///<summary> /// Inserts an answer from a person in the database /// </summary> public static void InsertAnswerFromPerson(Answer answer) { using (SurveyContext context = new SurveyContext()) { try { //When answer.Question is set, EF automagically cretare record in DB for Question as well answer.Question = null; context.Answers.Add(answer); context.SaveChanges(); } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } } }
public static void DeleteQuestion(int id) { using (SurveyContext context = new SurveyContext()) { context.Questions.FirstOrDefault(i => i.Id == id).Is_deleted = true; context.SaveChanges(); } }
///<summary> /// Inserts a question in the database /// </summary> public static void InsertQuestion(Question question) { using (SurveyContext context = new SurveyContext()) { context.Questions.Add(question); context.SaveChanges(); } }
///<summary> /// Inserts an origignal answer in the database /// </summary> public static void InsertOriginalAnswer(OriginalAnswer answer) { using (SurveyContext context = new SurveyContext()) { //When answer.Question is set, EF automagically cretare record in DB for Question as well answer.Question = null; context.OriginalAnswers.Add(answer); context.SaveChanges(); } }
///<summary> /// Edits a question by id from the database. Properties that can be editted: Question_text, Is_deleted, Start_date, End_date /// </summary> public static void EditQuestion(int id, Question editedQuestion) { using (SurveyContext context = new SurveyContext()) { Question question = GetQuestion(id); question.Question_text = editedQuestion.Question_text; question.Is_deleted = editedQuestion.Is_deleted; question.Start_date = editedQuestion.Start_date; question.End_date = editedQuestion.End_date; context.SaveChanges(); } }
///<summary> /// Edits original answers for a question from the database /// </summary> public static void EditOriginalAnswers(int questionId, string newOriginalAnswer1, string newOriginalAnswer2) { using (SurveyContext context = new SurveyContext()) { List <OriginalAnswer> originalAnswers = GetOriginalAnswers(questionId); if (originalAnswers.Count > 0) { originalAnswers[0].Answer_text = newOriginalAnswer1; } if (originalAnswers.Count > 1) { originalAnswers[1].Answer_text = newOriginalAnswer2; } context.SaveChanges(); } }