diff --git a/I18N Commander/DataModel/Database/Common/DataContextFactory.cs b/I18N Commander/DataModel/Database/Common/DataContextFactory.cs new file mode 100644 index 0000000..db940bd --- /dev/null +++ b/I18N Commander/DataModel/Database/Common/DataContextFactory.cs @@ -0,0 +1,28 @@ +using Microsoft.EntityFrameworkCore.Design; + +namespace DataModel.Database.Common; + +/// +/// This factory is used by the EF tooling e.g. to create migrations. +/// +public sealed class DataContextFactory : IDesignTimeDbContextFactory +{ + private const string ENV_EF_TOOLING_DATABASE = "ENV_EF_TOOLING_DATABASE"; + + #region Implementation of IDesignTimeDbContextFactory + + /// + public DataContext CreateDbContext(string[] args) + { + var dataFile = Environment.GetEnvironmentVariable(ENV_EF_TOOLING_DATABASE); + if (string.IsNullOrWhiteSpace(dataFile)) + { + Console.WriteLine("In order to use EF tooling, point the environment variable ENV_EF_TOOLING_DATABASE to the data file, which should be used for the EF tooling."); + Environment.Exit(100); + } + + return Setup.CreateDatabaseInstance4Tooling(dataFile, false); + } + + #endregion +} \ No newline at end of file diff --git a/I18N Commander/DataModel/Database/TextElement.cs b/I18N Commander/DataModel/Database/TextElement.cs index 7870238..538dac1 100644 --- a/I18N Commander/DataModel/Database/TextElement.cs +++ b/I18N Commander/DataModel/Database/TextElement.cs @@ -16,7 +16,7 @@ public sealed class TextElement public bool IsMultiLine { get; set; } = false; - public Section Section { get; set; } + public Section Section { get; set; } = new(); public List Translations { get; set; } = new(); diff --git a/I18N Commander/DataModel/Program.cs b/I18N Commander/DataModel/Program.cs deleted file mode 100644 index d07d548..0000000 --- a/I18N Commander/DataModel/Program.cs +++ /dev/null @@ -1,13 +0,0 @@ -using Microsoft.Extensions.Hosting; -namespace DataModel; - -public static class Program -{ - public static void Main(string[] args) - { - Console.WriteLine("This app is intended for the EF tooling. You cannot start this data project, though."); - Environment.Exit(0); - } - - public static IHostBuilder CreateHostBuilder(string[] args) => Setup.Setup4EFTooling(args); -} \ No newline at end of file diff --git a/I18N Commander/DataModel/Setup.cs b/I18N Commander/DataModel/Setup.cs index e687af1..05f33d6 100644 --- a/I18N Commander/DataModel/Setup.cs +++ b/I18N Commander/DataModel/Setup.cs @@ -9,7 +9,6 @@ namespace DataModel; public static class Setup { - private const string ENV_EF_TOOLING_DATABASE = "ENV_EF_TOOLING_DATABASE"; private const string DB_READ_WRITE_MODE = "ReadWrite"; private const string DB_READ_WRITE_CREATE_MODE = "ReadWriteCreate"; @@ -90,26 +89,26 @@ public static class Setup } /// - /// Sets up the DI & db context ready for the EF tooling. + /// Create the database instance from the given path. Used for the EF tooling. /// - public static IHostBuilder Setup4EFTooling(string[] args) + public static DataContext CreateDatabaseInstance4Tooling(string path2DataFile, bool createWhenNecessary = true) { - var dataFile = Environment.GetEnvironmentVariable(ENV_EF_TOOLING_DATABASE); - if (string.IsNullOrWhiteSpace(dataFile)) - { - Console.WriteLine("In order to use EF tooling, point the environment variable ENV_EF_TOOLING_DATABASE to the data file, which should be used for the EF tooling."); - Environment.Exit(100); - } + // Store the path to the database: + Setup.USED_DATA_FILE = path2DataFile; + Setup.SETUP_MAINTENANCE = new(path2DataFile, false); - var builder = Host.CreateDefaultBuilder(args); - builder.ConfigureServices((hostContext, serviceCollection) => - { - serviceCollection.AddDbContext(options => options.UseSqlite($"Filename={dataFile};Mode=ReadWriteCreate")); - }); - - return builder; + // Create a database builder: + var builder = new DbContextOptionsBuilder(); + + // 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; } + public readonly record struct SetupMaintenance(string PathToDataFile = "", bool RemoveTempDatabaseAfterwards = false) : IDisposable { public void Dispose()