private void Delete_Click(object sender, RoutedEventArgs e) { if (selectedProduct != null) { CodeFirst cf = new CodeFirst(); using (var transaction = cf.Database.BeginTransaction()) { try { var op = cf.OrdersProducts.Where(p => p.IDPRODUCT == selectedProduct.IDPRODUCT); PRODUCTS prod = cf.PRODUCTS.FirstOrDefault(p => p.IDPRODUCT == selectedProduct.IDPRODUCT); if (op != null && prod != null) { foreach (var x in op) { cf.OrdersProducts.Remove(x); } cf.PRODUCTS.Remove(prod); cf.SaveChanges(); transaction.Commit(); } else { MessageBox.Show("Ошибка при удалении"); transaction.Rollback(); } } catch (Exception ex) { transaction.Rollback(); } Products.Clear(); foreach (var x in cf.PRODUCTS) { Products.Add(x); } } } else { MessageBox.Show("Выберите продукт для удаления"); } }
private void Add_Click(object sender, RoutedEventArgs e) { PRODUCTS product = new PRODUCTS { NAME = this.name.Text, PRICE = int.Parse(this.price.Text), CURRENCY = this.currency.Text }; CodeFirst cf = new CodeFirst(); cf.PRODUCTS.Add(product); cf.SaveChanges(); Products.Clear(); foreach (var x in cf.PRODUCTS) { Products.Add(x); } }
private void Change_Click(object sender, RoutedEventArgs e) { CodeFirst cf = new CodeFirst(); if (selectedProduct != null) { PRODUCTS prod = cf.PRODUCTS.FirstOrDefault(p => p.IDPRODUCT == selectedProduct.IDPRODUCT); prod.NAME = this.name1.Text; prod.PRICE = int.Parse(this.price1.Text); prod.CURRENCY = this.currency1.Text; cf.SaveChanges(); Products.Clear(); foreach (var x in cf.PRODUCTS) { Products.Add(x); } } else { MessageBox.Show("Выберите товар для редактирования"); } }
public void Save() { db.SaveChanges(); }