//===================THIS METHOD IS FOR DELETE RECORDS IN DIRECTIONS TABLE============== public void DeleteDir() { using (StudentuConteiner db = new StudentuConteiner()) { try { var res = db.Orderlines; if (Dir.DirectionName != "---" && !CheckRecordBeforDelete(Dir)) { if (dialogService.YesNoDialog("Точно нужно удалить эту запись?") == true) { //changing DB //we find all the records in which we have the desired Id and make a replacement foreach (OrderLine order in res) { if (order.Direction.DirectionId == Dir.DirectionId) { order.Direction = db.Directions.Find(new Direction() { DirectionId = 1 }.DirectionId); } } db.Directions.Remove(db.Directions.Find(Dir.DirectionId)); db.SaveChanges(); //changing collection AuthorDirections.Remove(Dir); DirRecords.Remove(Dir); Dir = DirRecords[0]; SelectedDir2 = Dir; } } else { dialogService.ShowMessage("Нельзя удалить эту запись"); } } catch (ArgumentNullException ex) { dialogService.ShowMessage(ex.Message); } catch (OverflowException ex) { dialogService.ShowMessage(ex.Message); } catch (System.Data.SqlClient.SqlException ex) { dialogService.ShowMessage(ex.Message); } catch (System.Data.Entity.Core.EntityCommandExecutionException ex) { dialogService.ShowMessage(ex.Message); } catch (System.Data.Entity.Core.EntityException ex) { dialogService.ShowMessage(ex.Message); } } }