/// <summary> /// Creates a new file with the basic database structure for a Ringtoets database at /// <paramref name="databaseFilePath"/>. /// </summary> /// <param name="databaseFilePath">Path of the new database file.</param> /// <exception cref="ArgumentException">Thrown when either: /// <list type="bullet"> /// <item><paramref name="databaseFilePath"/> is invalid</item> /// <item><paramref name="databaseFilePath"/> points to an existing file</item> /// </list></exception> /// <exception cref="StorageException">Thrown when executing <c>DatabaseStructure</c> script fails.</exception> public static void CreateDatabaseStructure(string databaseFilePath) { IOUtils.ValidateFilePath(databaseFilePath); if (File.Exists(databaseFilePath)) { string message = $"File '{databaseFilePath}' already exists."; throw new ArgumentException(message); } SQLiteConnection.CreateFile(databaseFilePath); string connectionString = SqLiteConnectionStringBuilder.BuildSqLiteConnectionString(databaseFilePath, false); try { using (var dbContext = new SQLiteConnection(connectionString, true)) { dbContext.Open(); using (SQLiteCommand command = dbContext.CreateCommand()) { command.CommandText = Resources.TellTheStoryDatabaseSchema; command.ExecuteNonQuery(); } } } catch (SQLiteException exception) { string message = string.Format("Kon bestand \"{0}\" niet wegschrijven", databaseFilePath); throw new StorageException(message, exception); } finally { SQLiteConnection.ClearAllPools(); } }
/// <summary> /// Constructs a connection string to connect the Entity Framework to <paramref name="filePath"/>. /// </summary> /// <param name="filePath">Location of the storage file.</param> /// <returns>A new connection string.</returns> /// <exception cref="ArgumentNullException">Thrown when <paramref name="filePath"/> is <c>null</c> or empty (only whitespaces).</exception> public static string BuildSqLiteEntityConnectionString(string filePath) { if (string.IsNullOrWhiteSpace(filePath)) { throw new ArgumentNullException(nameof(filePath), @"Cannot create a connection string without the path to the file to connect to."); } return(new EntityConnectionStringBuilder { Metadata = string.Format(@"res://*/{0}.csdl|res://*/{0}.ssdl|res://*/{0}.msl", "DbContext.TellTheStoryEntityDataModel"), Provider = @"System.Data.SQLite.EF6", ProviderConnectionString = SqLiteConnectionStringBuilder.BuildSqLiteConnectionString(GetDataSourceLocation(filePath), false) }.ConnectionString); }