private static void AddBinaryQuestion( ApplicationDbContext dbContext, string quote, string questionableAuthor, string correctAuthor) { var quoteId = dbContext.Quotes.FirstOrDefault(q => q.Content == quote)?.Id; var questionableAuthorId = dbContext .Authors .FirstOrDefault(a => a.Name == questionableAuthor)? .Id; var correctAuthorId = dbContext .Authors .FirstOrDefault(a => a.Name == correctAuthor)? .Id; var binaryChoiceQuestion = new BinaryChoiceQuestion { QuoteId = quoteId.Value, QuestionableAuthorId = questionableAuthorId.Value, CorrectAuthorId = correctAuthorId.Value }; dbContext.BinaryChoiceQuestions.Add(binaryChoiceQuestion); dbContext.SaveChanges(true); }
private async Task <BinaryChoiceQuestion> CreateNewBinaryChoiceQuestionAsync(Fixture fixture) { _dbContext.BinaryChoiceQuestions.RemoveRange(_dbContext.BinaryChoiceQuestions); await _dbContext.SaveChangesAsync(); var questionableAuthor = new Author { Name = fixture.Create <string>() }; var correctAuthor = new Author { Name = fixture.Create <string>() }; await _dbContext.Authors.AddRangeAsync(questionableAuthor, correctAuthor); await _dbContext.SaveChangesAsync(); var quote = new Quote { Content = fixture.Create <string>() }; await _dbContext.Quotes.AddAsync(quote); await _dbContext.SaveChangesAsync(); var binaryChoiceQuestion = new BinaryChoiceQuestion { QuoteId = quote.Id, QuestionableAuthorId = questionableAuthor.Id, CorrectAuthorId = correctAuthor.Id }; await _dbContext.BinaryChoiceQuestions.AddAsync(binaryChoiceQuestion); await _dbContext.SaveChangesAsync(); return(binaryChoiceQuestion); }