private static void ValidateDatabaseVersion(RiskeerEntities riskeerEntities, string databaseFilePath) { try { string databaseVersion = riskeerEntities.VersionEntities.Select(v => v.Version).Single(); if (!ProjectVersionHelper.IsValidVersion(databaseVersion)) { string m = string.Format(Resources.StorageSqLite_ValidateDatabaseVersion_DatabaseVersion_0_is_invalid, databaseVersion); string message = new FileReaderErrorMessageBuilder(databaseFilePath).Build(m); throw new StorageValidationException(message); } if (ProjectVersionHelper.IsNewerThanCurrent(databaseVersion)) { string m = string.Format(Resources.StorageSqLite_ValidateDatabaseVersion_DatabaseVersion_0_higher_then_current_DatabaseVersion_1_, databaseVersion, ProjectVersionHelper.GetCurrentDatabaseVersion()); string message = new FileReaderErrorMessageBuilder(databaseFilePath).Build(m); throw new StorageValidationException(message); } } catch (InvalidOperationException e) { string message = new FileReaderErrorMessageBuilder(databaseFilePath).Build(Resources.StorageSqLite_ValidateDatabaseVersion_Database_must_have_one_VersionEntity_row); throw new StorageValidationException(message, e); } }
public bool HasStagedProjectChanges(string filePath) { if (!HasStagedProject) { throw new InvalidOperationException("Call 'StageProject(IProject)' first before calling this method."); } if (string.IsNullOrWhiteSpace(filePath)) { return(true); } string connectionString = GetConnectionToExistingFile(filePath); try { byte[] originalHash; using (var dbContext = new RiskeerEntities(connectionString)) originalHash = dbContext.VersionEntities.Select(v => v.FingerPrint).First(); byte[] hash = FingerprintHelper.Get(stagedProject.Entity); return(!FingerprintHelper.AreEqual(originalHash, hash)); } catch (CannotDetermineFingerprintException e) { if (e.InnerException is QuotaExceededException) { throw new StorageException(Resources.StorageSqLite_HasStagedProjectChanges_Project_contains_too_many_objects_to_generate_fingerprint, e); } throw new StorageException(e.Message, e); } }
public void Constructor_WithConnectionString_ExpectedValues() { // Setup string fullConnectionString = string.Format(CultureInfo.CurrentCulture, entityConnectionString, connectionString); // Call using (var entities = new RiskeerEntities(fullConnectionString)) { // Assert Assert.IsInstanceOf <System.Data.Entity.DbContext>(entities); Assert.AreEqual(connectionString, entities.Database.Connection.ConnectionString); Assert.IsFalse(entities.Configuration.LazyLoadingEnabled); } }
public IProject LoadProject(string databaseFilePath) { string connectionString = GetConnectionToExistingFile(databaseFilePath); try { RiskeerProject project; using (var dbContext = new RiskeerEntities(connectionString)) { ValidateDatabaseVersion(dbContext, databaseFilePath); dbContext.LoadTablesIntoContext(); ProjectEntity projectEntity; try { projectEntity = dbContext.ProjectEntities.Local.Single(); } catch (InvalidOperationException exception) { throw CreateStorageReaderException(databaseFilePath, Resources.StorageSqLite_LoadProject_Invalid_Riskeer_database_file, exception); } project = projectEntity.Read(new ReadConversionCollector()); } project.Name = Path.GetFileNameWithoutExtension(databaseFilePath); return(project); } catch (DataException exception) { throw CreateStorageReaderException(databaseFilePath, Resources.StorageSqLite_LoadProject_Invalid_Riskeer_database_file, exception); } catch (EntityReadException exception) { throw CreateStorageReaderException(databaseFilePath, exception.Message, exception); } catch (SystemException exception) { throw CreateStorageReaderException(databaseFilePath, Resources.StorageSqLite_LoadProject_Invalid_Riskeer_database_file, exception); } }
/// <summary> /// Sets the connection to the Riskeer database. /// </summary> /// <param name="databaseFilePath">The path of the file, which is used for creating exceptions.</param> /// <exception cref="StorageValidationException">Thrown when the database does not contain the table <c>version</c>.</exception> private static string GetConnectionToStorage(string databaseFilePath) { string connectionString = SqLiteEntityConnectionStringBuilder.BuildSqLiteEntityConnectionString(databaseFilePath); using (var dbContext = new RiskeerEntities(connectionString)) { try { dbContext.Database.Initialize(true); dbContext.VersionEntities.Load(); } catch (Exception exception) { string message = new FileReaderErrorMessageBuilder(databaseFilePath).Build(Resources.StorageSqLite_LoadProject_Invalid_Riskeer_database_file); throw new StorageValidationException(message, exception); } } return(connectionString); }
private void SaveProjectInDatabase(string databaseFilePath) { string connectionString = GetConnectionToNewFile(databaseFilePath); using (var dbContext = new RiskeerEntities(connectionString)) { try { dbContext.VersionEntities.Add(new VersionEntity { Version = ProjectVersionHelper.GetCurrentDatabaseVersion(), Timestamp = DateTime.Now, FingerPrint = FingerprintHelper.Get(stagedProject.Entity) }); dbContext.ProjectEntities.Add(stagedProject.Entity); dbContext.SaveChanges(); } catch (DataException exception) { throw CreateStorageWriterException(databaseFilePath, Resources.Error_saving_database, exception); } catch (CannotDetermineFingerprintException exception) { throw CreateStorageWriterException(databaseFilePath, exception.Message, exception); } catch (SystemException exception) { if (exception is InvalidOperationException || exception is NotSupportedException) { throw CreateStorageWriterException(databaseFilePath, Resources.Error_during_connection, exception); } throw; } stagedProject.Model.Name = Path.GetFileNameWithoutExtension(databaseFilePath); } }