protected virtual bool Insert() { var context = VanillaInventoryContext; var model = ConvertToEntity(); try { context.Add(model); context.SaveChanges(); } catch (DbUpdateException e) { string err; if (IsConstraintsViolation(e)) { err = "record already existed"; } else { err = e.InnerException?.Message ?? e.Message; } UniversalMessageBox.Show($"Unable to save: {err}", "Save failed", UniversalMessageBox.MessageBoxButton.OK, UniversalMessageBox.MessageBoxImage.Error); return(false); } return(true); }
private void Close() { if (ShouldPromptClose()) { var result = UniversalMessageBox.Show("Close without saving changes?", "Close", UniversalMessageBox.MessageBoxButton.YesNo, UniversalMessageBox.MessageBoxImage.Exclamation); if (result == UniversalMessageBox.MessageBoxResult.No) { return; } } Messenger.Default.Send(WindowMessages.CloseWindow, MsgToken); }
protected override bool Execute() { Debug.Assert(IntQuantity != null, nameof(IntQuantity) + " != null"); Debug.Assert(SelectedInventory != null, nameof(SelectedInventory) + " != null"); using (var transaction = _context.Database.BeginTransaction()) { var entity = new Transaction { Comments = NullOrWhitespaceAsNull(Comments), Direction = Transaction.InventoryDirection.Addition, Inventory = SelectedInventory, Price = null, Quantity = IntQuantity.Value, Supplier = null, Time = TransactionTime }; _context.Add(entity); SelectedInventory.Quantity += IntQuantity.Value; _context.Entry(SelectedInventory).State = EntityState.Modified; try { _context.SaveChanges(); transaction.Commit(); return(true); } catch (DbUpdateException e) { UniversalMessageBox.Show($"An error has occured: {e.Message}\n{e.InnerException?.Message}", "Save failed", UniversalMessageBox.MessageBoxButton.OK, UniversalMessageBox.MessageBoxImage.Error); transaction.Rollback(); UndoingChangesDbContextLevel(_context); return(false); } } }
protected bool DeleteSelectedItem() { var selectedItem = GetSelectedItems(); if (selectedItem != null) { var context = VanillaInventoryContext; context.RemoveRange(selectedItem); try { context.SaveChanges(); } catch (DbUpdateException e) { string err; if (IsConstraintsViolation(e)) { err = "used by other records"; } else { err = e.InnerException?.Message ?? e.Message; } UniversalMessageBox.Show($"Unable to save: {err}", "Save failed", UniversalMessageBox.MessageBoxButton.OK, UniversalMessageBox.MessageBoxImage.Error); return(false); } RemoveSelectedItemFromList(); ClearSelectedItems(); } return(true); }