public void Save() { PTCData db = null; Messages.Clear(); ResetException(); try { db = new PTCData(); // Ensure the correct category is set if (Entity.CategoryId == null) { Entity.Category = db.Categories.Find(Entity.Category.CategoryId); } else { Entity.Category = db.Categories.Find(Entity.CategoryId); } Entity.CategoryId = Entity.Category.CategoryId; // Either Update or Insert product if (PageMode == PDSAPageModeEnum.Edit) { db.Entry(Entity).State = EntityState.Modified; db.SaveChanges(); } else if (PageMode == PDSAPageModeEnum.Add) { db.Products.Add(Entity); db.SaveChanges(); } // Get all the data again in case anything changed Get(); } catch (DbEntityValidationException ex) { IsValid = false; // Validation errors foreach (var errors in ex.EntityValidationErrors) { foreach (var item in errors.ValidationErrors) { Messages.AddModelError(item.PropertyName, item.ErrorMessage); } } // Set page state SetUIState(PageMode); } catch (Exception ex) { LastException = ex; Message = "ERROR Saving Product"; } }
protected void Update() { PTCData db = null; try { db = new PTCData(); // Do editing here db.Entry(Entity).State = EntityState.Modified; db.SaveChanges(); PageMode = PDSAPageModeEnum.List; } catch (DbEntityValidationException ex) { IsValid = false; ValidationErrorsToMessages(ex); } catch (Exception ex) { Publish(ex, "Error Updating Product With ID=" + Entity.ProductId.ToString()); } }