コード例 #1
0
        internal void RemoveFromDb(DbSources sourceCat, int index)
        {
            switch (sourceCat)
            {
            case DbSources.Producers:
            {
                Producers producerToRemove = db.Producers.Single(x => x.producer_id == index);
                db.Producers.Remove(producerToRemove);
                db.SaveChanges();
                break;
            }

            case DbSources.Categories:
            {
                Categories categoryToRemove = db.Categories.Single(x => x.category_id == index);
                db.Categories.Remove(categoryToRemove);
                db.SaveChanges();
                break;
            }

            case DbSources.Products:
            {
                Products productToRemove = db.Products.Single(x => x.product_id == index);
                db.Products.Remove(productToRemove);
                db.SaveChanges();
                break;
            }

            case DbSources.Employees:
            {
                Employees employeeToRemove = db.Employees.Single(x => x.employee_id == index);
                db.Employees.Remove(employeeToRemove);
                db.SaveChanges();
                break;
            }

            case DbSources.Clients:
                break;
            }
        }
        private void Add_Click(object sender, RoutedEventArgs e)
        {
            string value = AddNameTB.Text.Trim();

            if (commander.IsInputInvalid(value, "Producer name"))
            {
                MessageBox.Show($"Can't add empty or default value to database.");
                return;
            }
            if (commander.ExistsInDatabaseByNameCaseInsensitive(OSHome.DbSources.Producers, value, out string found))
            {
                MessageBox.Show($"Producer with name {found} already exists in database.");
                return;
            }
            Producers newProducer = new Producers();

            newProducer.producer_name = value;
            db.Producers.Add(newProducer);
            db.SaveChanges();
            MessageBox.Show($"Successfully added new producer to database: {value}.");
            AddNameTB.Text = "Producer name";
        }