Files
systemrevivalserver/data/DesignTimeDbContextFactory.cs
T

35 lines
1.2 KiB
C#

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
namespace DataServer.Data;
public sealed class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<GameDbContext>
{
public GameDbContext CreateDbContext(string[] args)
{
string cs;
try { cs = ConnectionStringFactory.BuildMySqlFromEnv(); }
catch
{
var cfg = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true)
.AddUserSecrets<DesignTimeDbContextFactory>(optional: true)
.AddEnvironmentVariables()
.Build();
cs = cfg.GetConnectionString("PrimaryMySql")
?? cfg["Database:ConnectionString"]
?? "Server=localhost;Port=3306;Database=systemrevival;User Id=root;Password=pass;";
}
var opts = new DbContextOptionsBuilder<GameDbContext>()
// Keep design-time independent of live DB connectivity.
.UseMySql(cs, new MySqlServerVersion(new Version(8, 0, 36)))
.Options;
return new GameDbContext(opts);
}
}