//метод для action private void SaveOrUpdateProduct(Product product) { using (var context = new HwContext()) { if (selectedProduct != null) { var contextProduct = context.Products.FirstOrDefault(x => x.Id == selectedProduct.Id); contextProduct.Name = product.Name; contextProduct.CategoryId = product.CategoryId; context.SaveChanges(); return; } context.Products.Add(product); context.SaveChanges(); } }
private void UpdateBtnClick(object sender, RoutedEventArgs e) { using (var context = new HwContext()) { books.ToList().ForEach(x => context.Update(x)); context.SaveChanges(); } }
//метод для action private void SaveOrUpdateCategory(string name) { using (var context = new HwContext()) { if (selectedCategory != null) { var contextCategory = context.Categories.FirstOrDefault(x => x.Id == selectedCategory.Id); contextCategory.Name = name; context.SaveChanges(); return; } context.Categories.Add(new Category { Name = name }); context.SaveChanges(); categories = context.Categories.ToList(); } }
private void AddBtnClick(object sender, RoutedEventArgs e) { using (var context = new HwContext()) { int.TryParse(priceTB.Text, out var price); context.Add(new Book { Author = authorTB.Text, Price = price, TitleName = titleNameTB.Text }); priceTB.Text = ""; authorTB.Text = ""; titleNameTB.Text = ""; context.SaveChanges(); books = new ObservableCollection <Book>(context.Books.ToList()); dataGrid.ItemsSource = books; } }