private void button_RemovePressform_Click(object sender, EventArgs e) { if (comboBox_Pressform.SelectedValue == null) { return; } int id = (int)comboBox_Pressform.SelectedValue; try { using (DBContext dbContext = new DBContext()) { Pressform pressform = dbContext.Pressforms.Where(d => d.Id == id).Single(); int countPressforms = dbContext.Data.Where(d => d.Pressform.Id == id).Count(); if (countPressforms == 0) { dbContext.Pressforms.Remove(pressform); dbContext.SaveChanges(); } else { Common.ShowErrorMessage("Эта прессформа уже используется"); return; } } } catch (Exception ex) { Common.ShowErrorMessage(ex.ToString()); } Common.ShowInfoMessage("Прессформа удалена"); }
private void button_AddPressform_Click(object sender, EventArgs e) { if (comboBox_Pressform.SelectedValue == null) { return; } Pressform pressForm = new Pressform(); pressForm.Name = comboBox_Pressform.Text; using (DBContext dbContext = new DBContext()) { dbContext.Pressforms.Add(pressForm); try { dbContext.SaveChanges(); } catch (Exception ex) { Common.ShowErrorMessage(ex.ToString()); return; } } Common.ShowInfoMessage("Прессформа добавлена"); comboBox_Pressform.SelectedIndex = -1; }