Du burde være i stand til at gøre det ved at overføre en forbindelse til din DbContext
og tilslutte StateChange
begivenhed:(Tilgiv venligst SQLite-eksemplet. Jeg ved, du sagde PostgreSQL.)
var connection = new SqliteConnection(connectionString);
_connection.StateChange += (sender, e) =>
{
if (e.OriginalState != ConnectionState.Open)
return;
var senderConnection = (DbConnection)sender;
using (var command = senderConnection.CreateCommand())
{
command.Connection = senderConnection;
command.CommandText = "-- TODO: Put little SQL command here.";
command.ExecuteNonQuery();
}
};
optionsBuilder.UseSqlite(connection);