0
0

Don't use PooledDbContextFactory when initially loading config

This commit is contained in:
Rhys Ickeringill
2025-12-28 16:19:33 +11:00
parent d80dd87d66
commit b7848d71d6
6 changed files with 23 additions and 11 deletions

View File

@@ -84,9 +84,11 @@ public record Setting : ISetting
public required string Value { get; set; }
}
public class MyDbContext(DbContextOptions<MyDbContext> options) : DbContext(options), ISettingsDbContext<DbSet<Setting>, Setting>
public class MyDbContext(DbContextOptions<MyDbContext> options) : DbContext(options), ISettingsDbContext<DbSet<Setting>, Setting>, ISettingsDbContextFactory<MyDbContext>
{
public DbSet<Setting> Settings { get; set; }
public static MyDbContext Create(DbContextOptions<MyDbContext> options) => new(options);
}

View File

@@ -57,9 +57,11 @@ public record Setting : ISetting
public required string Value { get; set; }
}
public class MyDbContext(DbContextOptions<MyDbContext> options) : DbContext(options), ISettingsDbContext<DbSet<Setting>,Setting>
public class MyDbContext(DbContextOptions<MyDbContext> options) : DbContext(options), ISettingsDbContext<DbSet<Setting>, Setting>, ISettingsDbContextFactory<MyDbContext>
{
public DbSet<Setting> Settings { get; set; }
public static MyDbContext Create(DbContextOptions<MyDbContext> options) => new(options);
}

View File

@@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
@@ -26,7 +27,7 @@ internal class EntityFrameworkCoreDbSetConfigurationProvider<TDbContext, TSettin
public override void Load()
{
using var dbContext = _configurationSource.DbContextFactory!.CreateDbContext();
using var dbContext = _configurationSource.DbContextFactory();
Data = dbContext.Settings.ToDictionary(s => s.Key, s => (string?)s.Value);
}
@@ -37,14 +38,14 @@ internal class EntityFrameworkCoreDbSetConfigurationProvider<TDbContext, TSettin
internal interface IEntityFrameworkCoreDbSetConfigurationSource<TDbContext> where TDbContext : DbContext
{
internal IDbContextFactory<TDbContext> DbContextFactory { get; }
internal Func<TDbContext> DbContextFactory { get; }
}
internal class EntityFrameworkCoreDbSetConfigurationSource<TDbContext, TSetting> : IConfigurationSource, IEntityFrameworkCoreDbSetConfigurationSource<TDbContext>
where TDbContext : DbContext, ISettingsDbContext<DbSet<TSetting>, TSetting>
where TSetting : class, ISetting
{
public required IDbContextFactory<TDbContext> DbContextFactory { get; init; }
public required Func<TDbContext> DbContextFactory { get; init; }
public IConfigurationProvider Build(IConfigurationBuilder builder)
{

View File

@@ -1,5 +1,4 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.Configuration;
namespace RAIC.Extensions.Configuration.EntityFrameworkCore.Extensions;
@@ -21,13 +20,13 @@ public static class ConfigurationBuilderExtensions
/// </param>
/// <returns>The <see cref="IConfigurationBuilder"/></returns>
public static IConfigurationBuilder AddDbSet<TDbContext, TSetting>(this IConfigurationBuilder builder, DbContextOptionsTransformer<TDbContext> optionsTransformer)
where TDbContext : DbContext, ISettingsDbContext<DbSet<TSetting>, TSetting>
where TDbContext : DbContext, ISettingsDbContext<DbSet<TSetting>, TSetting>, ISettingsDbContextFactory<TDbContext>
where TSetting : class, ISetting
{
// DEBT: Find way to create non-pooled DbContextFactory since this is only a short lived usage
var options = optionsTransformer(new DbContextOptionsBuilder<TDbContext>()).Options;
var configurationSource = new EntityFrameworkCoreDbSetConfigurationSource<TDbContext, TSetting>()
{
DbContextFactory = new PooledDbContextFactory<TDbContext>(optionsTransformer(new DbContextOptionsBuilder<TDbContext>()).Options, poolSize: 1)
DbContextFactory = () => TDbContext.Create(options)
};
return builder.Add(configurationSource);

View File

@@ -11,6 +11,12 @@ public interface ISettingsDbContext<out TSettingDbSet, out TSetting> : IDisposab
TSettingDbSet Settings { get; }
}
public interface ISettingsDbContextFactory<TDbContext>
where TDbContext : DbContext
{
static abstract TDbContext Create(DbContextOptions<TDbContext> options);
}
public interface ISetting
{

View File

@@ -33,9 +33,11 @@ public record Setting : ISetting
public required string Value { get; set; }
}
public class MyDbContext(DbContextOptions<MyDbContext> options) : DbContext(options), ISettingsDbContext<DbSet<Setting>, Setting>
public class MyDbContext(DbContextOptions<MyDbContext> options) : DbContext(options), ISettingsDbContext<DbSet<Setting>, Setting>, ISettingsDbContextFactory<MyDbContext>
{
public DbSet<Setting> Settings { get; set; }
public static MyDbContext Create(DbContextOptions<MyDbContext> options) => new(options);
}
var builder = Host.CreateApplicationBuilder(args) // or WebApplication.CreateBuilder(args);