Skip to content

Commit

Permalink
Open a connection to the db if necessary.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTedder committed Jun 24, 2024
1 parent d20276d commit 784709f
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion LeaderboardBackend/Models/Entities/ApplicationContext.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Data;
using System.Reflection;
using Microsoft.EntityFrameworkCore;
using Npgsql;
Expand Down Expand Up @@ -31,9 +32,22 @@ public ApplicationContext(DbContextOptions<ApplicationContext> options)
public void MigrateDatabase()
{
Database.Migrate();
bool tempConnection = false;
NpgsqlConnection connection = (NpgsqlConnection)Database.GetDbConnection();

if (connection.State is ConnectionState.Closed)
{
tempConnection = true;
Database.OpenConnection();
}

// when new extensions have been enabled by migrations, Npgsql's type cache must be refreshed
((NpgsqlConnection)Database.GetDbConnection()).ReloadTypes();
connection.ReloadTypes();

if (tempConnection)
{
Database.CloseConnection();
}
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
Expand Down

0 comments on commit 784709f

Please sign in to comment.