Fix DbContext configuration nightmares.
Thanks to julian figuring out IDesignTimeDbContextFactory exists in #6327. All this DbContext configuration and options setup stuff is insane. Microsoft should be absolutely ashamed for coming up with this load of garbage.
This commit is contained in:
25
Content.Server.Database/DesignTimeContextFactories.cs
Normal file
25
Content.Server.Database/DesignTimeContextFactories.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Design;
|
||||
// ReSharper disable UnusedType.Global
|
||||
|
||||
namespace Content.Server.Database;
|
||||
|
||||
public sealed class DesignTimeContextFactoryPostgres : IDesignTimeDbContextFactory<PostgresServerDbContext>
|
||||
{
|
||||
public PostgresServerDbContext CreateDbContext(string[] args)
|
||||
{
|
||||
var optionsBuilder = new DbContextOptionsBuilder<PostgresServerDbContext>();
|
||||
optionsBuilder.UseNpgsql(args[0]);
|
||||
return new PostgresServerDbContext(optionsBuilder.Options);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class DesignTimeContextFactorySqlite : IDesignTimeDbContextFactory<SqliteServerDbContext>
|
||||
{
|
||||
public SqliteServerDbContext CreateDbContext(string[] args)
|
||||
{
|
||||
var optionsBuilder = new DbContextOptionsBuilder<SqliteServerDbContext>();
|
||||
optionsBuilder.UseSqlite(args[0]);
|
||||
return new SqliteServerDbContext(optionsBuilder.Options);
|
||||
}
|
||||
}
|
||||
@@ -11,22 +11,10 @@ namespace Content.Server.Database
|
||||
{
|
||||
public abstract class ServerDbContext : DbContext
|
||||
{
|
||||
/// <summary>
|
||||
/// The "dotnet ef" CLI tool uses the parameter-less constructor.
|
||||
/// When that happens we want to supply the <see cref="DbContextOptions"/> via <see cref="DbContext.OnConfiguring"/>.
|
||||
/// To use the context within the application, the options need to be passed the constructor instead.
|
||||
/// </summary>
|
||||
protected readonly bool InitializedWithOptions;
|
||||
|
||||
public ServerDbContext()
|
||||
protected ServerDbContext(DbContextOptions options) : base(options)
|
||||
{
|
||||
}
|
||||
|
||||
public ServerDbContext(DbContextOptions<ServerDbContext> options) : base(options)
|
||||
{
|
||||
InitializedWithOptions = true;
|
||||
}
|
||||
|
||||
public DbSet<Preference> Preference { get; set; } = null!;
|
||||
public DbSet<Profile> Profile { get; set; } = null!;
|
||||
public DbSet<AssignedUserId> AssignedUserId { get; set; } = null!;
|
||||
|
||||
@@ -8,21 +8,17 @@ namespace Content.Server.Database
|
||||
{
|
||||
public sealed class PostgresServerDbContext : ServerDbContext
|
||||
{
|
||||
// This is used by the "dotnet ef" CLI tool.
|
||||
public PostgresServerDbContext()
|
||||
{
|
||||
}
|
||||
|
||||
static PostgresServerDbContext()
|
||||
{
|
||||
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
|
||||
}
|
||||
|
||||
public PostgresServerDbContext(DbContextOptions<PostgresServerDbContext> options) : base(options)
|
||||
{
|
||||
}
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder options)
|
||||
{
|
||||
if (!InitializedWithOptions)
|
||||
options.UseNpgsql("dummy connection string");
|
||||
|
||||
options.ReplaceService<IRelationalTypeMappingSource, CustomNpgsqlTypeMappingSource>();
|
||||
|
||||
((IDbContextOptionsBuilderInfrastructure) options).AddOrUpdateExtension(new SnakeCaseExtension());
|
||||
@@ -41,10 +37,6 @@ namespace Content.Server.Database
|
||||
#endif
|
||||
}
|
||||
|
||||
public PostgresServerDbContext(DbContextOptions<ServerDbContext> options) : base(options)
|
||||
{
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
|
||||
@@ -13,15 +13,12 @@ namespace Content.Server.Database
|
||||
{
|
||||
public sealed class SqliteServerDbContext : ServerDbContext
|
||||
{
|
||||
public SqliteServerDbContext()
|
||||
public SqliteServerDbContext(DbContextOptions<SqliteServerDbContext> options) : base(options)
|
||||
{
|
||||
}
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder options)
|
||||
{
|
||||
if (!InitializedWithOptions)
|
||||
options.UseSqlite("dummy connection string");
|
||||
|
||||
((IDbContextOptionsBuilderInfrastructure) options).AddOrUpdateExtension(new SnakeCaseExtension());
|
||||
|
||||
options.ConfigureWarnings(x =>
|
||||
@@ -70,10 +67,6 @@ namespace Content.Server.Database
|
||||
.HasConversion(jsonConverter);
|
||||
}
|
||||
|
||||
public SqliteServerDbContext(DbContextOptions<ServerDbContext> options) : base(options)
|
||||
{
|
||||
}
|
||||
|
||||
private static string InetToString(IPAddress address, int mask) {
|
||||
if (address.IsIPv4MappedToIPv6)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user