public void DeleteCollection(object param) { try { using (var db = new AliContext()) { // Select group and items for deletion var gs = db.Groups.Where(g => g.Id == (Guid)param).Include(i => i.Items).ToList(); // Delete group foreach (var g in gs) { db.Groups.Remove(g); } db.SaveChanges(); // Delete group from datagrid // Get group by id var group = AliGroups.First(g => g.Id == (Guid)param); // Remove current group AliGroups.Remove(group); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void DoSaveDb(object sender, DoWorkEventArgs e) { // Show progress bar ProgressBar.Value = 0; ProgressBar.Content = ""; ProgressBar.Visibility = Visibility.Visible; // Send current progress to status view OnProgress?.Invoke(ProgressBar); // Disable buttons Buttons.IsEnabled = false; // Show progressbar ProgressBar.Visibility = Visibility.Visible; try { using (AliContext db = new AliContext()) { var group = new AliGroupModel(); group.Name = CollectionTitle; group.Created = DateTime.Now; group.Items = new ObservableCollection <AliItemModel>(); // Add all items int counter = 0; int itemsCount = AliItems.Count; foreach (var item in AliItems) { var itemModel = new AliItemModel { Title = item.Title, Price = item.Price, PriceCurrency = item.PriceCurrency, Unit = item.Unit, Seller = item.Seller, Link = item.Link, Description = item.Description, Image = item.Image }; group.Items.Add(itemModel); // Set progress bar value counter++; int percent = (int)(Convert.ToDouble(counter) / Convert.ToDouble(itemsCount) * 100); _bw1.ReportProgress(percent, String.Format("Saving item(s) {0} of {1}", counter, itemsCount)); } db.Groups.Add(group); db.SaveChanges(); } // Fire database changed event to refresh main window OnDbChanged?.Invoke(); // Show alert MessageBox.Show("Items successfully saved!", "Info", MessageBoxButton.OK, MessageBoxImage.Information); } catch (EntityException ex) { // Show message box MessageBox.Show("Unable to connect to database. \n \n" + "Check the database instance MS SQL Server LocalDB on your computer.\n \n" + ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error); } }