public async Task <Outcome> SaveOutcomeAsync(OutcomeModel outcomeModel) { //using (var transaction = await _db.Database.BeginTransactionAsync()) //{ Outcome outcome; if (outcomeModel.Id.HasValue) { outcome = await _db.Outcomes .Include(o => o.Photo) .FirstAsync(o => o.Id == outcomeModel.Id.Value); } else { outcome = new Outcome(); _db.Add(outcome); } outcome.Name = outcomeModel.Name; outcome.Summary = outcomeModel.Summary; outcome.PhotoId = outcomeModel.PhotoId; outcome.QuizId = outcomeModel.QuizId; var existingRoleOutcomes = _db.CharacterRoleOutcomes.Where(cro => cro.OutcomeId == outcome.Id); _db.RemoveRange(existingRoleOutcomes); await _db.SaveChangesAsync(); foreach (var roleOutcomeModel in outcomeModel.CharacterRoleOutcomes) { outcome.CharacterRoleOutcomes.Add(new CharacterRoleOutcome { CharacterRoleId = roleOutcomeModel.CharacterRoleId, OutcomeId = outcome.Id, Value = roleOutcomeModel.Value }); } await _db.SaveChangesAsync(); //transaction.Commit(); return(await _db.Outcomes .Include(o => o.CharacterRoleOutcomes) .ThenInclude(o => o.CharacterRole) .Include(o => o.AnswerOutcomes) .ThenInclude(o => o.Answer) .ThenInclude(o => o.Question) .FirstAsync(o => o.Id == outcome.Id)); //} }
public async Task <Answer> SaveAnswerAsync(AnswerModel answerModel) { Answer answer; if (answerModel.Id.HasValue) { answer = await _db.Answers .Include(a => a.AnswerOutcomes) .Include(a => a.Photo) .FirstAsync(a => a.Id == answerModel.Id.Value); } else { answer = new Answer(); _db.Add(answer); } answer.Text = answerModel.Text; answer.QuestionId = answerModel.QuestionId; answer.IsPhotoOnly = answerModel.IsPhotoOnly; answer.PhotoId = answerModel.PhotoId; await _db.SaveChangesAsync(); return(answer); }
public async Task <Photo> AddPhoto(Photo photo) { _db.Add(photo); await _db.SaveChangesAsync(); return(photo); }
public async Task <IActionResult> Create([Bind("QuestionId,QuestionText,CorrectAnswer,WrongAnswer1,WrongAnswer2,WrongAnswer3,WrongAnswer4")] Question question) { if (ModelState.IsValid) { _context.Add(question); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(question)); }
public async Task <Quiz> SaveQuizAsync(QuizModel quizModel) { Quiz quiz; if (quizModel.Id.HasValue) { quiz = await _db.Quizzes.FirstAsync(q => q.Id == quizModel.Id.Value); } else { quiz = new Quiz(); _db.Add(quiz); } quiz.Title = quizModel.Title.Trim(); quiz.Summary = quizModel.Summary; await _db.SaveChangesAsync(); return(quiz); }
public async Task <Question> SaveQuestionAsync(QuestionModel questionModel) { Question question; if (questionModel.Id.HasValue) { question = await _db.Questions.FirstAsync(q => q.Id == questionModel.Id.Value); } else { question = new Question(); _db.Add(question); } question.Text = questionModel.Text; question.Points = questionModel.Points; question.QuizId = questionModel.QuizId; await _db.SaveChangesAsync(); return(question); }
public void Add <T>(T entity) where T : class { _context.Add(entity); }
public void Add <T>(T entity) where T : class { db.Add(entity); }
public void Add <T>(T entity) where T : class { _logger.LogInformation($"Adding an object of type {entity.GetType()} to the context."); _context.Add(entity); }
public void Create(T entity) { _context.Add(entity); Save(); }