private void btnCadastro_Click(object sender, EventArgs e) { string nome = textBoxNome.Text.ToString(); string ip = textBoxIp.Text.ToString(); try { int id; using (var labContext = new LaboratorioContext(_DB)) { id = labContext.Laboratorios.FirstOrDefault(s => s.Nome.Contains(textBoxLab.Text.ToString())).Id; } using (var maqContext = new MaquinaContext(_DB)) { maqContext.Maquinas.Add(maq = new Maquina(nome, ip, id)); maqContext.SaveChanges(); MessageBox.Show("Realizado com sucesso!"); } } catch (Exception ex) { MessageBox.Show(ex.Message); MessageBox.Show(ex.StackTrace); } }
private void btnRemover_Click(object sender, EventArgs e) { try { using (var maqContext = new MaquinaContext(_DB)) { var id = maqContext.Maquinas.Where(s => s.Nome == comboBoxMaqs.SelectedItem.ToString()).FirstOrDefault().id; var Maq = maqContext.Maquinas.Where(s => s.id == id).FirstOrDefault(); maqContext.Maquinas.Remove(Maq); maqContext.SaveChanges(); MessageBox.Show("Realizado com sucesso!"); comboBoxMaqs.Items.RemoveAt(comboBoxMaqs.SelectedIndex); } } catch (Exception ex) { MessageBox.Show(ex.Message); MessageBox.Show(ex.StackTrace); } }
private void btnUpdate_Click(object sender, EventArgs e) { if (checkBoxNome.Checked) { try { using (var maqContext = new MaquinaContext(_DB)) { var item = maqContext.Maquinas.FirstOrDefault(c => c.Ip == textBoxIp.Text.ToString()); if (item != null) { item.MudarNome(textBoxNome.Text.ToString()); maqContext.Entry(item).State = System.Data.Entity.EntityState.Modified; maqContext.SaveChanges(); MessageBox.Show("Realizado com sucesso!"); } } } catch (Exception ex) { MessageBox.Show(ex.Message); MessageBox.Show(ex.StackTrace); } } if (checkBoxLab.Checked) { try { int id; using (var labContext = new LaboratorioContext(_DB)) { id = labContext.Laboratorios.Where(s => s.Nome.Contains(textBoxLab.Text.ToString())).FirstOrDefault().Id; } using (var maqContext = new MaquinaContext(_DB)) { var item = maqContext.Maquinas.FirstOrDefault(c => c.Ip == textBoxIp.Text.ToString()); if (item != null) { item.MudarIdLab(id); maqContext.Entry(item).State = System.Data.Entity.EntityState.Modified; maqContext.SaveChanges(); MessageBox.Show("Realizado com sucesso!"); } } } catch (Exception ex) { MessageBox.Show(ex.Message); MessageBox.Show(ex.StackTrace); } } if (checkBoxIp.Checked) { try { using (var maqContext = new MaquinaContext(_DB)) { var item = maqContext.Maquinas.FirstOrDefault(c => c.Nome == textBoxNome.Text.ToString()); if (item != null) { item.MudarIp(textBoxIp.Text.ToString()); maqContext.Entry(item).State = System.Data.Entity.EntityState.Modified; maqContext.SaveChanges(); MessageBox.Show("Realizado com sucesso!"); } } } catch (Exception ex) { MessageBox.Show(ex.Message); MessageBox.Show(ex.StackTrace); } } }