Foxnouns.NET/Foxnouns.Backend/Database/DatabaseServiceExtensions.cs
sam d982342ab8
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.
2024-10-30 15:35:23 +01:00

21 lines
586 B
C#

using Serilog;
namespace Foxnouns.Backend.Database;
public static class DatabaseServiceExtensions
{
public static IServiceCollection AddFoxnounsDatabase(
this IServiceCollection serviceCollection,
Config config
)
{
var dataSource = DatabaseContext.BuildDataSource(config);
var loggerFactory = new LoggerFactory().AddSerilog(dispose: false);
serviceCollection.AddDbContext<DatabaseContext>(options =>
DatabaseContext.BuildOptions(options, dataSource, loggerFactory)
);
return serviceCollection;
}
}