public async Task<IHttpActionResult> Post(Question question) { if (!ModelState.IsValid) { return BadRequest(ModelState); } context.Questions.Add(question); await context.SaveChangesAsync(); return Created(question); }
public void A() { using (var context = new ExamContext()) { Question q = new Question(); q.Type = QuestionType.SingleClause; q.Text = "Question text"; q.Answers.Add(new Answer() { Text = "Correct", Score = new Score() { Points = 10 } }); q.Answers.Add(new Answer() { Text = "Wrong1" }); q.Answers.Add(new Answer() { Text = "Wrong2" }); context.Questions.Add(q); context.SaveChanges(); } }
public void DoSave() { bool isMultiple = Answers.Count(a => a.Score != null) > 1; Question question = new Question() { Text = Text, MaxPoints = Answers.Where(a => a.Score != null).Sum(b => b.Score.Points), Type = isMultiple ? QuestionType.MultupleClause : QuestionType.SingleClause }; _client.Context.AddToQuestions(question); //_client.Context.SaveChanges(); foreach (var answer in Answers) { _client.Context.AddToAnswers(answer); _client.Context.AddLink(question, "Answers", answer); //_client.Context.SaveChanges(); if (answer.Score != null) { _client.Context.AddToScores(answer.Score); _client.Context.SetLink(answer, "Score", answer.Score); //_client.Context.SaveChanges(); } } _client.Context.SaveChanges(); }