public void SaveAnsweredQuiz(IEnumerable <Pregunta> preguntas, int deviceId) { using (TransactionScope scope = new TransactionScope()) { try { AnsweredQuizModel answeredQuiz = new AnsweredQuizModel(); answeredQuiz.QuizDeviceId = deviceId; answeredQuizRepository.Add(answeredQuiz); foreach (Pregunta pregunta in preguntas) { answeredQuizDetailRepository.Add(new AnsweredQuizDetailModel() { QuestionId = pregunta.IdPregunta, AnswerId = GetStartAnswerValue(pregunta.GetRespuesta()), AnsweredQuizId = answeredQuiz.AnsweredQuizId }); } scope.Complete(); } catch (Exception ex) { throw new Exception($"Error en repositorio: {ex.Message}"); } } }
public void Add(AnsweredQuizModel model) { try { using (MySqlConnection con = new MySqlConnection(_connectionString)) { string sql = "INSERT INTO encuesta.answeredquiz (quizDeviceId) VALUES(@quizDeviceId);"; using (MySqlCommand cmd = new MySqlCommand(sql, con)) { cmd.Parameters.AddWithValue("@quizDeviceId", model.QuizDeviceId); con.Open(); cmd.ExecuteNonQuery(); model.AnsweredQuizId = Convert.ToInt32(cmd.LastInsertedId); } } } catch (Exception ex) { throw new Exception(ex.Message, ex); } }