private void FlippedChangedCommand_Executed(object sender, ExecutedRoutedEventArgs e) { if ((e.Parameter as Flipper).IsFlipped) { using (ApplicationContext context = new ApplicationContext()) { ClinicVM.SelectedDoctor = new Doctor(); var Id = (int)(e.Parameter as Flipper).Tag; if (e.Parameter != null) { ClinicVM.SelectedDoctor = context.Doctors.Where(p => p.Id == Id).FirstOrDefault(); } //(e.Parameter as Flipper).DataContext = ClinicVM.SelectedDoctor; } } else { try { if (!(e.OriginalSource as Button).IsCancel) { if (ClinicVM.ImageFile.Count() > 0) { if (ClinicVM.SelectedDoctor != null) { var _doctor = db.Doctors.Where(p => p.Id == ClinicVM.SelectedDoctor.Id).FirstOrDefault(); _doctor.Image = ImgConverter.BitmapToByteArray(_doctor.ImageControl.Source, ClinicVM.ImageFile); } else { ClinicVM.FictionDoctor.Image = ImgConverter.BitmapToByteArray(ClinicVM.FictionDoctor.ImageControl.Source, ClinicVM.ImageFile); } } if (ClinicVM.SelectedDoctor != null) { db.Entry(ClinicVM.FictionDoctor).State = EntityState.Unchanged; } db.SaveChanges(); if (ClinicVM.SelectedDoctor != null) { db.Entry(ClinicVM.FictionDoctor).State = EntityState.Added; } else { // Добавим фиктивного доктора ClinicVM.FictionDoctor = new Doctor(); ClinicVM.FictionDoctor.Id = db.Doctors.Max(p => p.Id) + 1; db.Doctors.Add(ClinicVM.FictionDoctor); } ClinicVM.DoctorCount = db.Doctors.Count() - 1; } else { if (ClinicVM.SelectedDoctor != null) { var _doctor = db.Doctors.Where(p => p.Id == ClinicVM.SelectedDoctor.Id).FirstOrDefault(); _doctor.Name = ClinicVM.SelectedDoctor.Name; _doctor.Surname = ClinicVM.SelectedDoctor.Surname; _doctor.Patronymic = ClinicVM.SelectedDoctor.Patronymic; _doctor.Phone = ClinicVM.SelectedDoctor.Phone; _doctor.Image = ClinicVM.SelectedDoctor.Image; // Тут надо загружать фото из базы } else { ClinicVM.FictionDoctor.Name = null; ClinicVM.FictionDoctor.Surname = null; ClinicVM.FictionDoctor.Patronymic = null; ClinicVM.FictionDoctor.Phone = null; ClinicVM.FictionDoctor.Image = null; } } ClinicVM.SelectedDoctor = null; ClinicVM.ImageFile = ""; } catch (Exception ex) { logger.Error($"В методе: {ex.TargetSite} возникло исключение: { ex.Message}"); MessageBox.Show($"Ошибка сохранения в базе данных. Обратитесь к администратору.", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); } } }