You've already forked Extensions.Configuration.EntityFrameworkCore
Don't use PooledDbContextFactory when initially loading config
This commit is contained in:
@@ -84,9 +84,11 @@ public record Setting : ISetting
|
|||||||
public required string Value { get; set; }
|
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 DbSet<Setting> Settings { get; set; }
|
||||||
|
|
||||||
|
public static MyDbContext Create(DbContextOptions<MyDbContext> options) => new(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -57,9 +57,11 @@ public record Setting : ISetting
|
|||||||
public required string Value { get; set; }
|
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 DbSet<Setting> Settings { get; set; }
|
||||||
|
|
||||||
|
public static MyDbContext Create(DbContextOptions<MyDbContext> options) => new(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using System.Linq;
|
using System;
|
||||||
|
using System.Linq;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
|
|
||||||
@@ -26,7 +27,7 @@ internal class EntityFrameworkCoreDbSetConfigurationProvider<TDbContext, TSettin
|
|||||||
|
|
||||||
public override void Load()
|
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);
|
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 interface IEntityFrameworkCoreDbSetConfigurationSource<TDbContext> where TDbContext : DbContext
|
||||||
{
|
{
|
||||||
internal IDbContextFactory<TDbContext> DbContextFactory { get; }
|
internal Func<TDbContext> DbContextFactory { get; }
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class EntityFrameworkCoreDbSetConfigurationSource<TDbContext, TSetting> : IConfigurationSource, IEntityFrameworkCoreDbSetConfigurationSource<TDbContext>
|
internal class EntityFrameworkCoreDbSetConfigurationSource<TDbContext, TSetting> : IConfigurationSource, IEntityFrameworkCoreDbSetConfigurationSource<TDbContext>
|
||||||
where TDbContext : DbContext, ISettingsDbContext<DbSet<TSetting>, TSetting>
|
where TDbContext : DbContext, ISettingsDbContext<DbSet<TSetting>, TSetting>
|
||||||
where TSetting : class, ISetting
|
where TSetting : class, ISetting
|
||||||
{
|
{
|
||||||
public required IDbContextFactory<TDbContext> DbContextFactory { get; init; }
|
public required Func<TDbContext> DbContextFactory { get; init; }
|
||||||
|
|
||||||
public IConfigurationProvider Build(IConfigurationBuilder builder)
|
public IConfigurationProvider Build(IConfigurationBuilder builder)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
|
|
||||||
namespace RAIC.Extensions.Configuration.EntityFrameworkCore.Extensions;
|
namespace RAIC.Extensions.Configuration.EntityFrameworkCore.Extensions;
|
||||||
@@ -21,13 +20,13 @@ public static class ConfigurationBuilderExtensions
|
|||||||
/// </param>
|
/// </param>
|
||||||
/// <returns>The <see cref="IConfigurationBuilder"/></returns>
|
/// <returns>The <see cref="IConfigurationBuilder"/></returns>
|
||||||
public static IConfigurationBuilder AddDbSet<TDbContext, TSetting>(this IConfigurationBuilder builder, DbContextOptionsTransformer<TDbContext> optionsTransformer)
|
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
|
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>()
|
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);
|
return builder.Add(configurationSource);
|
||||||
|
|||||||
@@ -11,6 +11,12 @@ public interface ISettingsDbContext<out TSettingDbSet, out TSetting> : IDisposab
|
|||||||
TSettingDbSet Settings { get; }
|
TSettingDbSet Settings { get; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public interface ISettingsDbContextFactory<TDbContext>
|
||||||
|
where TDbContext : DbContext
|
||||||
|
{
|
||||||
|
static abstract TDbContext Create(DbContextOptions<TDbContext> options);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public interface ISetting
|
public interface ISetting
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -33,9 +33,11 @@ public record Setting : ISetting
|
|||||||
public required string Value { get; set; }
|
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 DbSet<Setting> Settings { get; set; }
|
||||||
|
|
||||||
|
public static MyDbContext Create(DbContextOptions<MyDbContext> options) => new(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
var builder = Host.CreateApplicationBuilder(args) // or WebApplication.CreateBuilder(args);
|
var builder = Host.CreateApplicationBuilder(args) // or WebApplication.CreateBuilder(args);
|
||||||
|
|||||||
Reference in New Issue
Block a user