public JsonResult Create(RevisionModel model) { if (!ModelState.IsValid) return Json(new { result = "Error"}); Revision revision = new Revision { CaseId = model.CaseId, UserId = User.Identity.GetUserId() }; SaveRevision(revision, model); return Json(new { result = "Success"}); }
private void SaveRevision(Revision revision, RevisionModel model) { db.Revisions.Add(revision); foreach (Mistakes mistake in model.Mistakes) { Mistakes newmistake = new Mistakes { Title = mistake.Title, Type = mistake.Type, RevisionId = revision.Id }; db.Mistakes.Add(newmistake); } try { db.SaveChanges(); } catch (Exception ex) { Type type = ex.GetType(); if (!type.Equals(typeof(DbUpdateException))) throw new Exception(string.Format("Unhandled exception")); Type dbc = ex.InnerException.GetType(); if (!dbc.Equals(typeof(UpdateException))) throw new Exception(string.Format("Unhandled exception")); UpdateException upEx = (UpdateException)ex.InnerException; Type sqlType = upEx.InnerException.GetType(); if (!sqlType.Equals(typeof(SqlException))) throw new Exception(string.Format("Unhandled exception")); SqlException sqlEx = (SqlException)upEx.InnerException; int number = sqlEx.Number; if (number == 2627) { revision.RegenerateId(); SaveRevision(revision, model); } else { throw new Exception(string.Format("Unhandled exception")); } } }