public int Update() { try { using (SurveyEntities dc = new SurveyEntities()) { //If the Id is set, get the result in the table where it matches if (this.Id != Guid.Empty) { tblResponse response = dc.tblResponses.FirstOrDefault(r => r.Id == this.Id); //If a row was retrieved, change if (response != null) { response.QuestionId = this.QuestionId; response.AnswerId = this.AnswerId; return(dc.SaveChanges()); } else { throw new Exception("Could not find Response row with this ID"); } } else { throw new Exception("Id not set on Response"); } } } catch (Exception ex) { throw ex; } }
public int Insert() { int result = 0; try { using (SurveyEntities dc = new SurveyEntities()) { //Answer new answer and set properties tblResponse response = new tblResponse(); response.Id = Guid.NewGuid(); response.QuestionId = this.QuestionId; response.AnswerId = this.AnswerId; this.Id = response.Id; //Add answer to the table dc.tblResponses.Add(response); //Commit the changes result = dc.SaveChanges(); return(result); } } catch (Exception ex) { throw ex; } }
public static bool Insert(Response response, bool rollback = false) { try { using (SurveyEntities dc = new SurveyEntities()) { DbContextTransaction transaction = null; if (rollback) { transaction = dc.Database.BeginTransaction(); } tblResponse newrow = new tblResponse() { Id = Guid.NewGuid(), QuestionId = response.QuestionId, AnswerId = response.AnswerId, ResponseDate = response.ResponseDate, }; dc.tblResponses.Add(newrow); response.Id = newrow.Id; if (rollback) { transaction.Rollback(); } return(dc.SaveChanges() == 1 ? true : false); } } catch (Exception ex) { throw ex; } }
public void InsertTest() { using (SurveyEntities dc = new SurveyEntities()) { tblResponse row = new tblResponse { Id = Guid.NewGuid(), AnswerId = dc.tblAnswers.FirstOrDefault(a => a.Answer == "Waffles").Id, QuestionId = dc.tblQuestions.FirstOrDefault(q => q.Question == "What color is #DFFF52").Id, ResponseDate = DateTime.Now }; dc.tblResponses.Add(row); Assert.AreEqual(1, dc.SaveChanges()); } }
public void DeleteTest() { using (SurveyEntities dc = new SurveyEntities()) { //get a question and answer tblAnswer answer = dc.tblAnswers.FirstOrDefault(a => a.Text == "Michael Scott"); tblQuestion question = dc.tblQuestions.FirstOrDefault(r => r.Text == "Who sprouts mung beans in their desk drawers?"); tblResponse response = dc.tblResponses.FirstOrDefault(r => (r.AnswerId == answer.Id) && (r.QuestionId == question.Id)); dc.tblResponses.Remove(response); int results = dc.SaveChanges(); Assert.IsTrue(results > 0); } }
public void InsertTest() { using (SurveyEntities dc = new SurveyEntities()) { //get a question and answer tblAnswer answer = dc.tblAnswers.FirstOrDefault(a => a.Text == "Kelly Kapoor"); tblQuestion question = dc.tblQuestions.FirstOrDefault(r => r.Text == "Who sprouts mung beans in their desk drawers?"); //set properties tblResponse response = new tblResponse(); response.Id = Guid.NewGuid(); response.QuestionId = question.Id; response.AnswerId = answer.Id; dc.tblResponses.Add(response); int results = dc.SaveChanges(); Assert.IsTrue(results > 0); } }