refactor: pass DbContextOptions into context directly

turns out efcore doesn't like it when we create a new options instance
(which includes a new data source *and* a new logger factory)
every single time we create a context. this commit extracts
OnConfiguring into static methods which are called when the context is
added to the service collection and when it's manually created for
migrations and the importer.
This commit is contained in:
sam 2024-10-30 15:35:23 +01:00
parent 0077a165b5
commit d982342ab8
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
5 changed files with 69 additions and 37 deletions

View file

@ -56,7 +56,11 @@ internal static class NetImporter
var loggerFactory = new LoggerFactory().AddSerilog(Log.Logger);
var config = new Config { Database = new Config.DatabaseConfig { Url = connString } };
var db = new DatabaseContext(config, loggerFactory);
var dataSource = DatabaseContext.BuildDataSource(config);
var options = DatabaseContext
.BuildOptions(new DbContextOptionsBuilder(), dataSource, loggerFactory)
.Options;
var db = new DatabaseContext(options);
if ((await db.Database.GetPendingMigrationsAsync()).Any())
{