/// <summary> /// Добавление элемента в базу данных /// </summary> /// <param name="obj">Любой преобразованный элемент из моделей БД</param> public static void AddObject(object obj) { ClientDatabaseContext context = null; try { if (User.DBMSName == "SQLite") { context = new ClientDatabaseContext(User.ConnectionString, true); } else { context = new ClientDatabaseContext(User.ConnectionString); } if (obj is Organization) { context.Organizations.Add((Organization)obj); } if (obj is Faculty) { context.Faculties.Add((Faculty)obj); } if (obj is Department) { context.Departments.Add((Department)obj); } if (obj is Group) { context.Groups.Add((Group)obj); } if (obj is Position) { context.Positions.Add((Position)obj); } if (obj is AcademicDegree) { context.AcademicDegrees.Add((AcademicDegree)obj); } if (obj is Author) { context.Authors.Add((Author)obj); } if (obj is Journal) { context.Journals.Add((Journal)obj); } if (obj is Conference) { context.Conferences.Add((Conference)obj); } if (obj is Work) { context.Works.Add((Work)obj); } if (obj is Direction) { context.Directions.Add((Direction)obj); } if (obj is Reward) { context.Rewards.Add((Reward)obj); } if (obj is CoAuthor) { context.CoAuthors.Add((CoAuthor)obj); } if (obj is DirectionWork) { context.DirectionWorks.Add((DirectionWork)obj); } if (obj is RewardWork) { context.RewardWorks.Add((RewardWork)obj); } context.SaveChanges(); IsDatabaseContextCreated = true; User.DateLastEditDatabase = DateTime.Now; ChangeDatabase?.Invoke(); } catch (Exception e) { ErrorManager.ExecuteException(new ErrorManager.NirsException("Ошибка при добавлении элемента", "База данных", "Работа с базой данных")); } finally { context?.Dispose(); } }
/// <summary> /// Удаление элемента /// </summary> /// <param name="obj">Любой преобразованный элемент из моделей БД</param> public static void DeleteObject(object obj) { if (!IsDatabaseContextCreated) { return; } ClientDatabaseContext context = null; try { if (User.DBMSName == "SQLite") { context = new ClientDatabaseContext(User.ConnectionString, true); } else { context = new ClientDatabaseContext(User.ConnectionString); } if (obj is Organization org) { var query = context.Organizations.FirstOrDefault(u => u.OrganizationId == org.OrganizationId); if (query != null) { context.Organizations.Remove(query); } } if (obj is Faculty fac) { var query = context.Faculties.FirstOrDefault(u => u.FacultyId == fac.FacultyId); if (query != null) { context.Faculties.Remove(query); } } if (obj is Department dep) { var query = context.Departments.FirstOrDefault(u => u.DepartmentId == dep.DepartmentId); if (query != null) { context.Departments.Remove(query); } } if (obj is Group gro) { var query = context.Groups.FirstOrDefault(u => u.GroupId == gro.GroupId); if (query != null) { context.Groups.Remove(query); } } if (obj is Position pos) { var query = context.Positions.FirstOrDefault(u => u.PositionId == pos.PositionId); if (query != null) { context.Positions.Remove(query); } } if (obj is AcademicDegree aca) { var query = context.AcademicDegrees.FirstOrDefault(u => u.AcademicDegreeId == aca.AcademicDegreeId); if (query != null) { context.AcademicDegrees.Remove(query); } } if (obj is Author aut) { var query = context.Authors.FirstOrDefault(u => u.AuthorId == aut.AuthorId); if (query != null) { context.Authors.Remove(query); } } if (obj is Journal jou) { var query = context.Journals.FirstOrDefault(u => u.JournalId == jou.JournalId); if (query != null) { context.Journals.Remove(query); } } if (obj is Conference con) { var query = context.Conferences.FirstOrDefault(u => u.ConferenceId == con.ConferenceId); if (query != null) { context.Conferences.Remove(query); } } if (obj is Work wor) { var query = context.Works.FirstOrDefault(u => u.WorkId == wor.WorkId); if (query != null) { context.Works.Remove(query); } } if (obj is Direction dir) { var query = context.Directions.FirstOrDefault(u => u.DirectionId == dir.DirectionId); if (query != null) { context.Directions.Remove(query); } } if (obj is Reward rew) { var query = context.Rewards.FirstOrDefault(u => u.RewardId == rew.RewardId); if (query != null) { context.Rewards.Remove(query); } } if (obj is CoAuthor coa) { var query = context.CoAuthors.FirstOrDefault(u => u.CoAuthorId == coa.CoAuthorId); if (query != null) { context.CoAuthors.Remove(query); } } if (obj is DirectionWork diw) { var query = context.DirectionWorks.FirstOrDefault(u => u.DirectionWorkId == diw.DirectionWorkId); if (query != null) { context.DirectionWorks.Remove(query); } } if (obj is RewardWork elem) { var query = context.RewardWorks.FirstOrDefault(u => u.RewardWorkId == elem.RewardWorkId); if (query != null) { context.RewardWorks.Remove(query); } } context.SaveChanges(); User.DateLastEditDatabase = DateTime.Now; ChangeDatabase?.Invoke(); } catch (Exception) { ErrorManager.ExecuteException(new ErrorManager.NirsException("Ошибка при удалении элемента", "База данных", "Работа с базой данных")); } finally { context?.Dispose(); } }