Added creation of database instance

This commit is contained in:
Thorsten Sommer 2022-08-17 21:00:58 +02:00
parent d53966501a
commit 7b149417e7
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108

View File

@ -37,6 +37,25 @@ public static class Setup
Setup.usedDataFile = path2DataFile; Setup.usedDataFile = path2DataFile;
serviceCollection.AddDbContext<DataContext>(options => options.UseSqlite($"Filename={path2DataFile};Mode={(createWhenNecessary ? DB_READ_WRITE_CREATE_MODE : DB_READ_WRITE_MODE)};"), ServiceLifetime.Transient); serviceCollection.AddDbContext<DataContext>(options => options.UseSqlite($"Filename={path2DataFile};Mode={(createWhenNecessary ? DB_READ_WRITE_CREATE_MODE : DB_READ_WRITE_MODE)};"), ServiceLifetime.Transient);
} }
/// <summary>
/// Create the database instance from the given path.
/// </summary>
public static DataContext CreateDatabaseInstance(string path2DataFile, bool createWhenNecessary = true)
{
// Store the path to the database:
Setup.usedDataFile = path2DataFile;
// Create a database builder:
var builder = new DbContextOptionsBuilder<DataContext>();
// Add the database configuration to the builder:
builder.UseSqlite($"Filename={path2DataFile};Mode={(createWhenNecessary ? DB_READ_WRITE_CREATE_MODE : DB_READ_WRITE_MODE)};");
// Next, construct the database context:
var dbContext = new DataContext(builder.Options);
return dbContext;
}
/// <summary> /// <summary>
/// Sets up the DI & db context ready for the EF tooling. /// Sets up the DI & db context ready for the EF tooling.