34 lines
1.1 KiB
C#
34 lines
1.1 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Design;
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
namespace Server.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>()
|
|
.UseMySql(cs, ServerVersion.AutoDetect(cs))
|
|
.Options;
|
|
|
|
return new GameDbContext(opts);
|
|
}
|
|
}
|
|
|