Пример #1
0
        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("Выберите продукт для удаления");
            }
        }
Пример #2
0
        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);
            }
        }
Пример #3
0
        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("Выберите товар для редактирования");
            }
        }
Пример #4
0
 public void Save()
 {
     db.SaveChanges();
 }