Fixed EF Tooling

- Added factory class for EF tooling
- Removed no longer needed Program.cs
This commit is contained in:
Thorsten Sommer 2023-01-22 19:56:25 +01:00
parent 0587af18f2
commit c41d000bf0
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
4 changed files with 44 additions and 30 deletions

View File

@ -0,0 +1,28 @@
using Microsoft.EntityFrameworkCore.Design;
namespace DataModel.Database.Common;
/// <summary>
/// This factory is used by the EF tooling e.g. to create migrations.
/// </summary>
public sealed class DataContextFactory : IDesignTimeDbContextFactory<DataContext>
{
private const string ENV_EF_TOOLING_DATABASE = "ENV_EF_TOOLING_DATABASE";
#region Implementation of IDesignTimeDbContextFactory<out DataContext>
/// <inheritdoc />
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
}

View File

@ -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<Translation> Translations { get; set; } = new();

View File

@ -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);
}

View File

@ -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
}
/// <summary>
/// Sets up the DI & db context ready for the EF tooling.
/// Create the database instance from the given path. Used for the EF tooling.
/// </summary>
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<DataContext>(options => options.UseSqlite($"Filename={dataFile};Mode=ReadWriteCreate"));
});
return builder;
// 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;
}
public readonly record struct SetupMaintenance(string PathToDataFile = "", bool RemoveTempDatabaseAfterwards = false) : IDisposable
{
public void Dispose()