Fixed EF Tooling
- Added factory class for EF tooling - Removed no longer needed Program.cs
This commit is contained in:
parent
0587af18f2
commit
c41d000bf0
@ -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
|
||||||
|
}
|
@ -16,7 +16,7 @@ public sealed class TextElement
|
|||||||
|
|
||||||
public bool IsMultiLine { get; set; } = false;
|
public bool IsMultiLine { get; set; } = false;
|
||||||
|
|
||||||
public Section Section { get; set; }
|
public Section Section { get; set; } = new();
|
||||||
|
|
||||||
public List<Translation> Translations { get; set; } = new();
|
public List<Translation> Translations { get; set; } = new();
|
||||||
|
|
||||||
|
@ -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);
|
|
||||||
}
|
|
@ -9,7 +9,6 @@ namespace DataModel;
|
|||||||
|
|
||||||
public static class Setup
|
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_MODE = "ReadWrite";
|
||||||
private const string DB_READ_WRITE_CREATE_MODE = "ReadWriteCreate";
|
private const string DB_READ_WRITE_CREATE_MODE = "ReadWriteCreate";
|
||||||
|
|
||||||
@ -90,26 +89,26 @@ public static class Setup
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <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>
|
/// </summary>
|
||||||
public static IHostBuilder Setup4EFTooling(string[] args)
|
public static DataContext CreateDatabaseInstance4Tooling(string path2DataFile, bool createWhenNecessary = true)
|
||||||
{
|
{
|
||||||
var dataFile = Environment.GetEnvironmentVariable(ENV_EF_TOOLING_DATABASE);
|
// Store the path to the database:
|
||||||
if (string.IsNullOrWhiteSpace(dataFile))
|
Setup.USED_DATA_FILE = path2DataFile;
|
||||||
{
|
Setup.SETUP_MAINTENANCE = new(path2DataFile, false);
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
var builder = Host.CreateDefaultBuilder(args);
|
// Create a database builder:
|
||||||
builder.ConfigureServices((hostContext, serviceCollection) =>
|
var builder = new DbContextOptionsBuilder<DataContext>();
|
||||||
{
|
|
||||||
serviceCollection.AddDbContext<DataContext>(options => options.UseSqlite($"Filename={dataFile};Mode=ReadWriteCreate"));
|
|
||||||
});
|
|
||||||
|
|
||||||
return builder;
|
// 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 readonly record struct SetupMaintenance(string PathToDataFile = "", bool RemoveTempDatabaseAfterwards = false) : IDisposable
|
||||||
{
|
{
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
|
Loading…
Reference in New Issue
Block a user