public async Task <EditSituationViewModel> UpdateSituation(int id, EditSituationViewModel newSituation) { try { Situations oldSituation = _context.Situations.Where (os => os.Id == id) .Select(os => os).First(); oldSituation.Deleted = true; _context.Entry(oldSituation).State = EntityState.Modified; _context.SaveChanges(); // 2.) insert new situation based on newSituationViewModel Situations situation = new Situations(); situation.Elevation = Convert.ToString(newSituation.Elevation); situation.LineType = newSituation.LineType; situation.Name = newSituation.Name; situation.PinCoordinate = newSituation.PinCoordinate; situation.StartCoordinate = newSituation.StartCoordinate; situation.TargetCoordinate = newSituation.TargetCoordinate; situation.WindDirection = newSituation.WindDirection; situation.WindSpeed = newSituation.WindSpeed; situation.IsFirstHole = newSituation.IsFirstHole; situation.NextHoleSituationId = newSituation.NextHoleSituationId; situation.YdsToPin = newSituation.YdsToPin; situation.VoiceOverUrl = newSituation.VoiceOverUrl; situation.IsBegining = newSituation.VoiceOverUrl == null?2: newSituation.IsBegining; situation.SituationCategory = _context.SituationCategories.Where (c => c.Id == newSituation.SituationCategoryId) .Select(c => c).First(); situation.Book = _context.PictureBooks.Where (b => b.Id == newSituation.BookId) .Select(b => b).First(); situation.ImageFile = _context.PictureBooks.Where (i => i.Id == newSituation.ImageFileId) .Select(i => i).First(); var sid = _context.Situations.Add(situation); //sid.Id; _context.SaveChanges(); //NewSituationAttributes var sa = newSituation.AttributesId.ToList(); for (int i = 0; i < sa.Count; i++) { SituationAttributes situationAttributes = new SituationAttributes(); situationAttributes.AttributesId = sa[i]; situationAttributes.SituationId = sid.Entity.Id; _context.SituationAttributes.Add(situationAttributes); _context.SaveChanges(); } //NewSituationHandicap var sh = newSituation.HandicapId.ToList(); for (int i = 0; i < sh.Count; i++) { SituationHandicaps situationHandicap = new SituationHandicaps(); situationHandicap.HandicapId = sh[i]; situationHandicap.SituationId = sid.Entity.Id; var h = _context.SituationHandicaps.Add(situationHandicap); _context.SaveChanges(); } //NewSituationQuestions - NewSituationAnswers var q = newSituation.Questions.ToList(); for (int i = 0; i < q.Count; i++) { SituationQuestions situationQuestions = new SituationQuestions(); situationQuestions.Question = q[i].Question; situationQuestions.SituationId = sid.Entity.Id; situationQuestions.SituationId1 = sid.Entity.Id; var qes = _context.SituationQuestions.Add(situationQuestions); _context.SaveChanges(); var a = q[i].Answers.ToList(); for (int j = 0; j < a.Count; j++) { Answers answers = new Answers(); answers.Answer = a[j].Answer; answers.DidYouKnowLink = a[j].DidYouKnowLink; answers.LessonLink = a[j].LessonLink; answers.Response = a[j].Response; answers.RulesLink = a[j].RulesLink; answers.Score = a[j].Score; answers.SituationQuestionsId = qes.Entity.Id; answers.Qid = qes.Entity.Id; _context.Answers.Add(answers); _context.SaveChanges(); } } newSituation.Id = situation.Id; return(newSituation); } catch (System.Exception ex) { throw ex; } }
public bool Update(int id, T data) { entities.Entry(data).State = EntityState.Modified; return(true); }