public _AuthorStatus() { AuthorStatus = new AuthorStatus(); SelectedAuthorStatus = new AuthorStatus(); AuthorStatusRecords = new ObservableCollection <AuthorStatus>(); LoadData(); showWindow = new DefaultShowWindowService(); dialogService = new DefaultDialogService(); }
//===================THIS METHOD IS FOR ADD RECORDS IN STATUS TABLES============== public void AddAuthorStatus() { using (StudentuConteiner db = new StudentuConteiner()) { try { var res4 = db.AuthorStatuses.Any(o => o.AuthorStatusName == AuthorStatus.AuthorStatusName); if (!res4) { if (!string.IsNullOrEmpty(AuthorStatus.AuthorStatusName)) { AuthorStatus.AuthorStatusName = AuthorStatus.AuthorStatusName.ToLower(); AuthorStatus.AuthorStatusName.Trim(); if (AuthorStatus.AuthorStatusName[0] == ' ') { dialogService.ShowMessage("Нельзя добавить пустую строку"); return; } db.AuthorStatuses.Add(AuthorStatus); db.SaveChanges(); AuthorStatusRecords.Clear(); LoadData(); AuthorStatus = new AuthorStatus(); } else { return; } } 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); } } }
//check if the record has links with other tables before deleting private bool CheckRecordBeforDelete(AuthorStatus authorStatus) { using (StudentuConteiner db = new StudentuConteiner()) { try { var res = db.Authors.ToList(); foreach (Author item in res) { if (item.AuthorStatus.AuthorStatusId == authorStatus.AuthorStatusId || item.AuthorStatus.AuthorStatusName == authorStatus.AuthorStatusName) { return(true); } } return(false); } 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); } } //есть подозрение, что такой подход не очень то правомерен, но пока лень с этим заморачиваться return(false); }