private void relacionar(object sender, EventArgs e) { try { bool existe = false; produto_ingrediente novo = new produto_ingrediente(); bd.ingrediente.ToList().ForEach(b => { if (b.nome.Equals(txtIngrediente.Text)) { novo.id_ingrediente = b.id; } }); bd.produto.ToList().ForEach(c => { if (c.nome.Equals(txtProduto.Text)) { novo.id_produto = c.id; } }); novo.quantidade = Convert.ToDecimal(txtQuantidade.Value); bd.produto_ingrediente.ToList().ForEach(jb => { if (jb.id_produto == novo.id_produto && jb.id_ingrediente == novo.id_ingrediente) { existe = true; } }); if (existe == false) { bd.produto_ingrediente.Add(novo); bd.SaveChanges(); txtIngrediente.Text = ""; txtProduto.Text = ""; MessageBox.Show("A relação entre o produto e o ingrediente foi concluída"); } else { MessageBox.Show("Essa relação já existe"); } } catch { MessageBox.Show("Erro ao relacionar"); } }
private void excluir(object sender, EventArgs e) { bd.produto.ToList().ForEach(z => { if (z.nome.Equals(txtDescrição.Text)) { produto db = z; bd.produto.Remove(db); bd.produto_ingrediente.ToList().ForEach(a => { if (a.id_produto == z.id) { produto_ingrediente aka = a; bd.produto_ingrediente.Remove(aka); } }); } }); bd.SaveChanges(); MessageBox.Show("Produto excluído!"); carregarTabelaProdutos(); }