/*public Question GetNextQuestionFromDB() * { * using (var context = new ExamSystem()) * { * var questions = context.Questions.Where(x => x.IsAnswered == false).OrderBy(o => o.Id); * * return questions.FirstOrDefault(); * } * }*/ public void CreateStudentIfNotExists(Student student) { using (var context = new OnlineExamSystem()) { var std = context.Students.Where(x => x.StudentId == student.StudentId).SingleOrDefault(); if (std == null) { context.Students.Add(student); var questions = context.Questions; var answers = new List <Answer>(); foreach (var question in questions) { var answer = new Answer { StudentId = student.Id, QuestionId = question.Id, IsAnswered = false }; answers.Add(answer); } context.Answers.AddRange(answers); context.SaveChanges(); } } }
public void InsertAnswerIntoDatabase(Question question) { using (var context = new OnlineExamSystem()) { context.Questions.Add(question); context.SaveChanges(); } }
public void UpdateStudent(Student student) { using (var context = new OnlineExamSystem()) { var std = context.Students.Where(x => x.StudentId == student.StudentId).SingleOrDefault(); if (std != null) { context.Students.Remove(std); context.Students.Add(student); context.SaveChanges(); } } }