You've already forked Extensions.Configuration.EntityFrameworkCore
33 lines
1.7 KiB
C#
33 lines
1.7 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
namespace RAIC.Extensions.Configuration.EntityFrameworkCore.Extensions;
|
|
|
|
public static class ConfigurationBuilderExtensions
|
|
{
|
|
public delegate DbContextOptionsBuilder<T> DbContextOptionsTransformer<T>(DbContextOptionsBuilder<T> transform) where T : DbContext;
|
|
|
|
/// <summary>
|
|
/// Adds a <see cref="DbSet{}"/> off <typeparamref name="TDbContext"/> as a configuration provider to the <see cref="IConfigurationBuilder"/>.
|
|
/// </summary>
|
|
/// <typeparam name="TDbContext">Type of the <see cref="DbContext"/> which implements both <see cref="ISettingsDbContext{}"/> and <see cref="ISettingsDbContextFactory{}"/></typeparam>
|
|
/// <param name="optionsTransformer">
|
|
/// a <see cref="DbContextOptionsTransformer{}"/> which configures your <see cref="DbContextOptions{}"/>. eg.
|
|
/// <code>
|
|
/// dbContextOptions => dbContextOptions.UseNpgsql(builder.Configuration.GetConnectionString("Default"))
|
|
/// </code>
|
|
/// </param>
|
|
/// <returns>The <see cref="IConfigurationBuilder"/></returns>
|
|
public static IConfigurationBuilder AddDbContext<TDbContext>(this IConfigurationBuilder builder, DbContextOptionsTransformer<TDbContext> optionsTransformer)
|
|
where TDbContext : DbContext, ISettingsDbContext<System.Linq.IQueryable<ISetting>>, ISettingsDbContextFactory<TDbContext>
|
|
{
|
|
var options = optionsTransformer(new DbContextOptionsBuilder<TDbContext>()).Options;
|
|
var configurationSource = new EntityFrameworkCoreDbSetConfigurationSource<TDbContext>()
|
|
{
|
|
DbContextFactory = () => TDbContext.Create(options)
|
|
};
|
|
|
|
return builder.Add(configurationSource);
|
|
}
|
|
}
|