private void deleteStudentButton_Click(object sender, RoutedEventArgs e) { if (studentsGrid.SelectedItems.Count > 0) { for (int i = 0; i < studentsGrid.SelectedItems.Count; i++) { WFAEntity.API.Student objectStudent = studentsGrid.SelectedItems[i] as WFAEntity.API.Student; if (objectStudent != null) { try { using (WFAEntity.API.MyDBContext objectMyDBContext = new WFAEntity.API.MyDBContext()) { WFAEntity.API.Student student = WFAEntity.API.DatabaseRequest.GetStudentById(objectMyDBContext, objectStudent.Id); objectMyDBContext.Students.Attach(student); objectMyDBContext.Students.Remove(student); objectMyDBContext.SaveChanges(); } this.ShowAll(SELECTED_TAB.STUDENT); } catch (Exception ex) { MessageBox.Show(ex.Message, "ОШИБКА", MessageBoxButton.OK, MessageBoxImage.Error); } } } } }
public StudentAddEditWindow(bool add_edit, int id = 0) { InitializeComponent(); this.add_edit = add_edit; this.id = id; using (WFAEntity.API.MyDBContext objectMyDBContext = new WFAEntity.API.MyDBContext()) { if (this.add_edit == false) { WFAEntity.API.Student objectStudent = WFAEntity.API.DatabaseRequest.GetStudentById(objectMyDBContext, this.id); textBlockAddEditSurname.Text = objectStudent.Surname; textBlockAddEditName.Text = objectStudent.Name; textBlockAddEditPatranomyc.Text = objectStudent.Patronymic; ButtonAddEditGroup.Content = "Изменить"; } comboBoxAddEditGroup.ItemsSource = WFAEntity.API.DatabaseRequest.GetGroups(objectMyDBContext); comboBoxAddEditGroup.Text = "{Binging Name}"; } }
private void ButtonAddEditGroup_Click(object sender, RoutedEventArgs e) { try { if (this.IsDataCorrect() == true) { using (WFAEntity.API.MyDBContext objectMyDBContext = new WFAEntity.API.MyDBContext()) { WFAEntity.API.Student objectStudent = new WFAEntity.API.Student( textBlockAddEditName.Text, textBlockAddEditSurname.Text, textBlockAddEditPatranomyc.Text, (WFAEntity.API.Group)comboBoxAddEditGroup.SelectedItem ); if (this.add_edit == true) { objectMyDBContext.Students.Add(objectStudent); } else { objectStudent.Id = WFAEntity.API.DatabaseRequest.GetStudentById(objectMyDBContext, this.id).Id; WFAEntity.API.Student objectStudentFromDataBase = new WFAEntity.API.Student(); objectStudentFromDataBase = WFAEntity.API.DatabaseRequest.GetStudentById(objectMyDBContext, this.id); objectMyDBContext.Entry(objectStudentFromDataBase).CurrentValues.SetValues(objectStudent); objectMyDBContext.SaveChanges(); } objectMyDBContext.SaveChanges(); this.DialogResult = true; } } } catch (Exception ex) { MessageBox.Show(ex.Message, "ОШИБКА (Студент)", MessageBoxButton.OK, MessageBoxImage.Error); } }