public void SaveChanges(EyetrackerEntities db) { int countNew = 0; int countChanged = 0; int countMissing = 0; foreach (BaseQuestionAnswer qa in this.Where(qa => qa.QuestionEntity.type_cd != "O")) { if (qa.AnswerEntity != null) { DbEntityEntry entry = null; if (qa.AnswerEntity.id == 0) { entry = db.Entry(db.Answer.Add(qa.AnswerEntity)); } else { entry = db.Entry(qa.AnswerEntity); if (entry == null) { entry = db.Entry(db.Answer.Attach(qa.AnswerEntity)); } } switch (entry.State) { case EntityState.Modified: countChanged++; break; case EntityState.Added: countNew++; break; } } else { countMissing++; } } if (countNew + countChanged > 0) { if (Test.LastStep < Questionnaire.Step) { Test.LastStep = Questionnaire.Step; if (Test.NumSteps == Test.LastStep) { Test.end_time = DateTime.Now; Test.status_cd = "TRM"; } else if (Test.status_cd == "NEW") { Test.status_cd = "PRG"; } db.SaveChanges(); } } }
public bool HasChanges(EyetrackerEntities db) { foreach (BaseQuestionAnswer qa in this.Where(qa => qa.QuestionEntity.type_cd != "O")) { if (qa.AnswerEntity != null && (db.Entry(qa.AnswerEntity) == null || db.Entry(qa.AnswerEntity).State != System.Data.Entity.EntityState.Unchanged)) { return(true); } } return(false); }