Add configuration actions for DbContext options builders (#6110)

* Add configuration actions for DbContext options builders

Introduce a configuration delegate parameter for each database provider's `Use*` methods, enhancing customization. This change affects MySql, SqlServer, Sqlite, PostgreSql, and Oracle providers, allowing more flexible DbContext configuration.

* Add support for connection strings as functions

This update introduces new overloads to allow passing connection strings as functions that take IServiceProvider. This enhances flexibility by enabling dynamic connection string resolution at runtime. All major database providers (MySQL, SQL Server, SQLite, PostgreSQL, and Oracle) have been updated accordingly.
This commit is contained in:
Sipke Schoorstra 2024-11-11 19:01:41 +01:00 committed by GitHub
parent 51793d08a6
commit 103034667c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 186 additions and 102 deletions

View file

@ -1,4 +1,5 @@
using System.Reflection;
using Microsoft.EntityFrameworkCore.Infrastructure;
// ReSharper disable once CheckNamespace
namespace Elsa.EntityFrameworkCore.Extensions;
@ -10,18 +11,49 @@ public static class MySqlProvidersExtensions
{
private static Assembly Assembly => typeof(MySqlProvidersExtensions).Assembly;
public static TFeature UseMySql<TFeature, TDbContext>(this PersistenceFeatureBase<TFeature, TDbContext> feature, string connectionString, ElsaDbContextOptions? options = null)
public static TFeature UseMySql<TFeature, TDbContext>(this PersistenceFeatureBase<TFeature, TDbContext> feature,
string connectionString,
ElsaDbContextOptions? options = null,
Action<MySqlDbContextOptionsBuilder>? configure = null)
where TDbContext : ElsaDbContextBase
where TFeature : PersistenceFeatureBase<TFeature, TDbContext>
{
return feature.UseMySql(Assembly, connectionString, options);
return feature.UseMySql(Assembly, connectionString, options, configure);
}
public static TFeature UseMySql<TFeature, TDbContext>(this PersistenceFeatureBase<TFeature, TDbContext> feature, Assembly migrationsAssembly, string connectionString, ElsaDbContextOptions? options = null)
public static TFeature UseMySql<TFeature, TDbContext>(this PersistenceFeatureBase<TFeature, TDbContext> feature,
Func<IServiceProvider, string> connectionStringFunc,
ElsaDbContextOptions? options = null,
Action<MySqlDbContextOptionsBuilder>? configure = null
)
where TDbContext : ElsaDbContextBase
where TFeature : PersistenceFeatureBase<TFeature, TDbContext>
{
feature.DbContextOptionsBuilder = (_, db) => db.UseElsaMySql(Assembly, connectionString, options);
return feature.UseMySql(Assembly, connectionStringFunc, options, configure);
}
public static TFeature UseMySql<TFeature, TDbContext>(this PersistenceFeatureBase<TFeature, TDbContext> feature,
Assembly migrationsAssembly,
string connectionString,
ElsaDbContextOptions? options = null,
Action<MySqlDbContextOptionsBuilder>? configure = null
)
where TDbContext : ElsaDbContextBase
where TFeature : PersistenceFeatureBase<TFeature, TDbContext>
{
return feature.UseMySql(migrationsAssembly, _ => connectionString, options, configure);
}
public static TFeature UseMySql<TFeature, TDbContext>(this PersistenceFeatureBase<TFeature, TDbContext> feature,
Assembly migrationsAssembly,
Func<IServiceProvider, string> connectionStringFunc,
ElsaDbContextOptions? options = null,
Action<MySqlDbContextOptionsBuilder>? configure = null
)
where TDbContext : ElsaDbContextBase
where TFeature : PersistenceFeatureBase<TFeature, TDbContext>
{
feature.DbContextOptionsBuilder = (sp, db) => db.UseElsaMySql(migrationsAssembly, connectionStringFunc(sp), options, configure: configure);
return (TFeature)feature;
}
}

View file

@ -1,80 +0,0 @@
using System.Reflection;
using Elsa.EntityFrameworkCore.Modules.Alterations;
using Elsa.EntityFrameworkCore.Modules.Identity;
using Elsa.EntityFrameworkCore.Modules.Labels;
using Elsa.EntityFrameworkCore.Modules.Management;
using Elsa.EntityFrameworkCore.Modules.Runtime;
// ReSharper disable once CheckNamespace
namespace Elsa.EntityFrameworkCore.Extensions;
/// <summary>
/// Provides extensions to configure EF Core to use Oracle.
/// </summary>
public static class OracleProvidersExtensions
{
private static Assembly Assembly => typeof(OracleProvidersExtensions).Assembly;
/// <summary>
/// Configures the <see cref="EFCoreIdentityPersistenceFeature"/> to use Oracle.
/// </summary>
public static EFCoreIdentityPersistenceFeature UseOracle(this EFCoreIdentityPersistenceFeature feature, string connectionString, ElsaDbContextOptions? options = default)
{
feature.DbContextOptionsBuilder = (_, db) => db.UseElsaOracle(Assembly, connectionString, options);
return feature;
}
/// <summary>
/// Configures the <see cref="EFCoreAlterationsPersistenceFeature"/> to use Oracle.
/// </summary>
public static EFCoreAlterationsPersistenceFeature UseOracle(this EFCoreAlterationsPersistenceFeature feature, string connectionString, ElsaDbContextOptions? options = default)
{
feature.DbContextOptionsBuilder = (_, db) => db.UseElsaOracle(Assembly, connectionString, options);
return feature;
}
/// <summary>
/// Configures the <see cref="EFCoreLabelPersistenceFeature"/> to use Oracle.
/// </summary>
public static EFCoreLabelPersistenceFeature UseOracle(this EFCoreLabelPersistenceFeature feature, string connectionString, ElsaDbContextOptions? options = default)
{
feature.DbContextOptionsBuilder = (_, db) => db.UseElsaOracle(Assembly, connectionString, options);
return feature;
}
/// <summary>
/// Configures the <see cref="EFCoreWorkflowDefinitionPersistenceFeature"/> to use Oracle.
/// </summary>
public static EFCoreWorkflowDefinitionPersistenceFeature UseOracle(this EFCoreWorkflowDefinitionPersistenceFeature feature, string connectionString, ElsaDbContextOptions? options = default)
{
feature.DbContextOptionsBuilder = (_, db) => db.UseElsaOracle(Assembly, connectionString, options);
return feature;
}
/// <summary>
/// Configures the <see cref="EFCoreWorkflowInstancePersistenceFeature"/> to use Oracle.
/// </summary>
public static EFCoreWorkflowInstancePersistenceFeature UseOracle(this EFCoreWorkflowInstancePersistenceFeature feature, string connectionString, ElsaDbContextOptions? options = default)
{
feature.DbContextOptionsBuilder = (_, db) => db.UseElsaOracle(Assembly, connectionString, options);
return feature;
}
/// <summary>
/// Configures the <see cref="WorkflowManagementPersistenceFeature"/> to use Oracle.
/// </summary>
public static WorkflowManagementPersistenceFeature UseOracle(this WorkflowManagementPersistenceFeature feature, string connectionString, ElsaDbContextOptions? options = default)
{
feature.DbContextOptionsBuilder = (_, db) => db.UseElsaOracle(Assembly, connectionString, options);
return feature;
}
/// <summary>
/// Configures the <see cref="EFCoreWorkflowRuntimePersistenceFeature"/> to use Oracle.
/// </summary>
public static EFCoreWorkflowRuntimePersistenceFeature UseOracle(this EFCoreWorkflowRuntimePersistenceFeature feature, string connectionString, ElsaDbContextOptions? options = default)
{
feature.DbContextOptionsBuilder = (_, db) => db.UseElsaOracle(Assembly, connectionString, options);
return feature;
}
}

View file

@ -0,0 +1,59 @@
using System.Reflection;
using Oracle.EntityFrameworkCore.Infrastructure;
// ReSharper disable once CheckNamespace
namespace Elsa.EntityFrameworkCore.Extensions;
/// <summary>
/// Provides extensions to configure EF Core to use Oracle.
/// </summary>
public static class OracleProvidersExtensions
{
private static Assembly Assembly => typeof(OracleProvidersExtensions).Assembly;
public static TFeature UseMySql<TFeature, TDbContext>(this PersistenceFeatureBase<TFeature, TDbContext> feature,
string connectionString,
ElsaDbContextOptions? options = null,
Action<OracleDbContextOptionsBuilder>? configure = null)
where TDbContext : ElsaDbContextBase
where TFeature : PersistenceFeatureBase<TFeature, TDbContext>
{
return feature.UseMySql(Assembly, connectionString, options, configure);
}
public static TFeature UseMySql<TFeature, TDbContext>(this PersistenceFeatureBase<TFeature, TDbContext> feature,
Func<IServiceProvider, string> connectionStringFunc,
ElsaDbContextOptions? options = null,
Action<OracleDbContextOptionsBuilder>? configure = null
)
where TDbContext : ElsaDbContextBase
where TFeature : PersistenceFeatureBase<TFeature, TDbContext>
{
return feature.UseMySql(Assembly, connectionStringFunc, options, configure);
}
public static TFeature UseMySql<TFeature, TDbContext>(this PersistenceFeatureBase<TFeature, TDbContext> feature,
Assembly migrationsAssembly,
string connectionString,
ElsaDbContextOptions? options = null,
Action<OracleDbContextOptionsBuilder>? configure = null
)
where TDbContext : ElsaDbContextBase
where TFeature : PersistenceFeatureBase<TFeature, TDbContext>
{
return feature.UseMySql(migrationsAssembly, _ => connectionString, options, configure);
}
public static TFeature UseMySql<TFeature, TDbContext>(this PersistenceFeatureBase<TFeature, TDbContext> feature,
Assembly migrationsAssembly,
Func<IServiceProvider, string> connectionStringFunc,
ElsaDbContextOptions? options = null,
Action<OracleDbContextOptionsBuilder>? configure = null
)
where TDbContext : ElsaDbContextBase
where TFeature : PersistenceFeatureBase<TFeature, TDbContext>
{
feature.DbContextOptionsBuilder = (sp, db) => db.UseElsaOracle(migrationsAssembly, connectionStringFunc(sp), options, configure: configure);
return (TFeature)feature;
}
}

View file

@ -1,4 +1,5 @@
using System.Reflection;
using Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure;
// ReSharper disable once CheckNamespace
namespace Elsa.EntityFrameworkCore.Extensions;
@ -9,19 +10,47 @@ namespace Elsa.EntityFrameworkCore.Extensions;
public static class PostgreSqlProvidersExtensions
{
private static Assembly Assembly => typeof(PostgreSqlProvidersExtensions).Assembly;
public static TFeature UsePostgreSql<TFeature, TDbContext>(this PersistenceFeatureBase<TFeature, TDbContext> feature, string connectionString, ElsaDbContextOptions? options = null)
public static TFeature UsePostgreSql<TFeature, TDbContext>(this PersistenceFeatureBase<TFeature, TDbContext> feature,
string connectionString,
ElsaDbContextOptions? options = null,
Action<NpgsqlDbContextOptionsBuilder>? configure = null)
where TDbContext : ElsaDbContextBase
where TFeature : PersistenceFeatureBase<TFeature, TDbContext>
{
return feature.UsePostgreSql(Assembly, connectionString, options);
return feature.UsePostgreSql(Assembly, connectionString, options, configure);
}
public static TFeature UsePostgreSql<TFeature, TDbContext>(this PersistenceFeatureBase<TFeature, TDbContext> feature, Assembly migrationsAssembly, string connectionString, ElsaDbContextOptions? options = null)
public static TFeature UsePostgreSql<TFeature, TDbContext>(this PersistenceFeatureBase<TFeature, TDbContext> feature,
Func<IServiceProvider, string> connectionStringFunc,
ElsaDbContextOptions? options = null,
Action<NpgsqlDbContextOptionsBuilder>? configure = null)
where TDbContext : ElsaDbContextBase
where TFeature : PersistenceFeatureBase<TFeature, TDbContext>
{
feature.DbContextOptionsBuilder = (_, db) => db.UseElsaPostgreSql(migrationsAssembly, connectionString, options);
return feature.UsePostgreSql(Assembly, connectionStringFunc, options, configure);
}
public static TFeature UsePostgreSql<TFeature, TDbContext>(this PersistenceFeatureBase<TFeature, TDbContext> feature,
Assembly migrationsAssembly,
string connectionString,
ElsaDbContextOptions? options = null,
Action<NpgsqlDbContextOptionsBuilder>? configure = null)
where TDbContext : ElsaDbContextBase
where TFeature : PersistenceFeatureBase<TFeature, TDbContext>
{
return feature.UsePostgreSql(migrationsAssembly, _ => connectionString, options, configure);
}
public static TFeature UsePostgreSql<TFeature, TDbContext>(this PersistenceFeatureBase<TFeature, TDbContext> feature,
Assembly migrationsAssembly,
Func<IServiceProvider, string> connectionStringFunc,
ElsaDbContextOptions? options = null,
Action<NpgsqlDbContextOptionsBuilder>? configure = null)
where TDbContext : ElsaDbContextBase
where TFeature : PersistenceFeatureBase<TFeature, TDbContext>
{
feature.DbContextOptionsBuilder = (sp, db) => db.UseElsaPostgreSql(migrationsAssembly, connectionStringFunc(sp), options, configure: configure);
return (TFeature)feature;
}
}

View file

@ -1,4 +1,5 @@
using System.Reflection;
using Microsoft.EntityFrameworkCore.Infrastructure;
// ReSharper disable once CheckNamespace
namespace Elsa.EntityFrameworkCore.Extensions;
@ -10,18 +11,46 @@ public static class SqlServerProvidersExtensions
{
private static Assembly Assembly => typeof(SqlServerProvidersExtensions).Assembly;
public static TFeature UseSqlServer<TFeature, TDbContext>(this PersistenceFeatureBase<TFeature, TDbContext> feature, string connectionString, ElsaDbContextOptions? options = null)
public static TFeature UseSqlServer<TFeature, TDbContext>(this PersistenceFeatureBase<TFeature, TDbContext> feature,
string connectionString,
ElsaDbContextOptions? options = null,
Action<SqlServerDbContextOptionsBuilder>? configure = null)
where TDbContext : ElsaDbContextBase
where TFeature : PersistenceFeatureBase<TFeature, TDbContext>
{
return feature.UseSqlServer(Assembly, connectionString, options);
return feature.UseSqlServer(Assembly, connectionString, options, configure);
}
public static TFeature UseSqlServer<TFeature, TDbContext>(this PersistenceFeatureBase<TFeature, TDbContext> feature, Assembly migrationsAssembly, string connectionString, ElsaDbContextOptions? options = null)
public static TFeature UseSqlServer<TFeature, TDbContext>(this PersistenceFeatureBase<TFeature, TDbContext> feature,
Func<IServiceProvider, string> connectionStringFunc,
ElsaDbContextOptions? options = null,
Action<SqlServerDbContextOptionsBuilder>? configure = null)
where TDbContext : ElsaDbContextBase
where TFeature : PersistenceFeatureBase<TFeature, TDbContext>
{
feature.DbContextOptionsBuilder = (_, db) => db.UseElsaSqlServer(migrationsAssembly, connectionString, options);
return feature.UseSqlServer(Assembly, connectionStringFunc, options, configure);
}
public static TFeature UseSqlServer<TFeature, TDbContext>(this PersistenceFeatureBase<TFeature, TDbContext> feature,
Assembly migrationsAssembly,
string connectionString,
ElsaDbContextOptions? options = null,
Action<SqlServerDbContextOptionsBuilder>? configure = null)
where TDbContext : ElsaDbContextBase
where TFeature : PersistenceFeatureBase<TFeature, TDbContext>
{
return feature.UseSqlServer(migrationsAssembly, _ => connectionString, options, configure);
}
public static TFeature UseSqlServer<TFeature, TDbContext>(this PersistenceFeatureBase<TFeature, TDbContext> feature,
Assembly migrationsAssembly,
Func<IServiceProvider, string> connectionStringFunc,
ElsaDbContextOptions? options = null,
Action<SqlServerDbContextOptionsBuilder>? configure = null)
where TDbContext : ElsaDbContextBase
where TFeature : PersistenceFeatureBase<TFeature, TDbContext>
{
feature.DbContextOptionsBuilder = (sp, db) => db.UseElsaSqlServer(migrationsAssembly, connectionStringFunc(sp), options, configure);
return (TFeature)feature;
}
}

View file

@ -1,5 +1,6 @@
using System.Reflection;
using Elsa.EntityFrameworkCore.EntityHandlers;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
// ReSharper disable once CheckNamespace
@ -15,43 +16,57 @@ public static class SqliteProvidersExtensions
/// <summary>
/// Configures the feature to use Sqlite.
/// </summary>
public static TFeature UseSqlite<TFeature, TDbContext>(this PersistenceFeatureBase<TFeature, TDbContext> feature, string? connectionString = null, ElsaDbContextOptions? options = null)
public static TFeature UseSqlite<TFeature, TDbContext>(this PersistenceFeatureBase<TFeature, TDbContext> feature,
string? connectionString = null,
ElsaDbContextOptions? options = null,
Action<SqliteDbContextOptionsBuilder>? configure = null)
where TDbContext : ElsaDbContextBase
where TFeature : PersistenceFeatureBase<TFeature, TDbContext>
{
return feature.UseSqlite(Assembly, connectionString, options);
return feature.UseSqlite(Assembly, connectionString, options, configure);
}
/// <summary>
/// Configures the feature to use Sqlite.
/// </summary>
public static TFeature UseSqlite<TFeature, TDbContext>(this PersistenceFeatureBase<TFeature, TDbContext> feature, Func<IServiceProvider, string> connectionStringFunc, ElsaDbContextOptions? options = null)
public static TFeature UseSqlite<TFeature, TDbContext>(this PersistenceFeatureBase<TFeature, TDbContext> feature,
Func<IServiceProvider, string> connectionStringFunc,
ElsaDbContextOptions? options = null,
Action<SqliteDbContextOptionsBuilder>? configure = null)
where TDbContext : ElsaDbContextBase
where TFeature : PersistenceFeatureBase<TFeature, TDbContext>
{
return feature.UseSqlite(Assembly, connectionStringFunc, options);
return feature.UseSqlite(Assembly, connectionStringFunc, options, configure);
}
/// <summary>
/// Configures the feature to use Sqlite.
/// </summary>
public static TFeature UseSqlite<TFeature, TDbContext>(this PersistenceFeatureBase<TFeature, TDbContext> feature, Assembly migrationsAssembly, string? connectionString = null, ElsaDbContextOptions? options = null)
public static TFeature UseSqlite<TFeature, TDbContext>(this PersistenceFeatureBase<TFeature, TDbContext> feature,
Assembly migrationsAssembly,
string? connectionString = null,
ElsaDbContextOptions? options = null,
Action<SqliteDbContextOptionsBuilder>? configure = null)
where TDbContext : ElsaDbContextBase
where TFeature : PersistenceFeatureBase<TFeature, TDbContext>
{
connectionString ??= "Data Source=elsa.sqlite.db;Cache=Shared;";
return feature.UseSqlite(migrationsAssembly, _ => connectionString, options);
return feature.UseSqlite(migrationsAssembly, _ => connectionString, options, configure);
}
/// <summary>
/// Configures the feature to use Sqlite.
/// </summary>
public static TFeature UseSqlite<TFeature, TDbContext>(this PersistenceFeatureBase<TFeature, TDbContext> feature, Assembly migrationsAssembly, Func<IServiceProvider, string> connectionStringFunc, ElsaDbContextOptions? options = null)
public static TFeature UseSqlite<TFeature, TDbContext>(this PersistenceFeatureBase<TFeature, TDbContext> feature,
Assembly migrationsAssembly,
Func<IServiceProvider, string> connectionStringFunc,
ElsaDbContextOptions? options = null,
Action<SqliteDbContextOptionsBuilder>? configure = null)
where TDbContext : ElsaDbContextBase
where TFeature : PersistenceFeatureBase<TFeature, TDbContext>
{
feature.Module.Services.AddScoped<IEntityModelCreatingHandler, SetupForSqlite>();
feature.DbContextOptionsBuilder = (sp, db) => db.UseElsaSqlite(migrationsAssembly, connectionStringFunc(sp), options);
feature.DbContextOptionsBuilder = (sp, db) => db.UseElsaSqlite(migrationsAssembly, connectionStringFunc(sp), options, configure: configure);
return (TFeature)feature;
}
}