From f203825ab5ca9f7ac0334a848d0c488b136a6930 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?SAL=C4=B0H=20=C3=96ZKARA?= <58659931+salihozkara@users.noreply.github.com> Date: Fri, 24 Oct 2025 14:49:29 +0300 Subject: [PATCH] Add extensibility for DbContext OnConfiguring actions Introduced DefaultOnConfiguringAction and OnConfiguringActions in AbpDbContextOptions to allow configuration of actions to be executed during DbContext.OnConfiguring. Updated AbpDbContext to invoke these actions, enabling more flexible and modular configuration of DbContext options. --- .../Abp/EntityFrameworkCore/AbpDbContext.cs | 6 ++++ .../AbpDbContextOptions.cs | 30 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs index 4b79bf017c..c773f527d6 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs @@ -117,6 +117,12 @@ public abstract class AbpDbContext : DbContext, IAbpEfCoreDbContext, { optionsBuilder.ConfigureWarnings(c => c.Ignore(RelationalEventId.PendingModelChangesWarning)); base.OnConfiguring(optionsBuilder); + + Options.Value.DefaultOnConfiguringAction?.Invoke(this, optionsBuilder); + foreach (var onConfiguringAction in Options.Value.OnConfiguringActions.GetOrDefault(typeof(TDbContext)) ?? []) + { + onConfiguringAction.As>().Invoke(this, optionsBuilder); + } } protected override void OnModelCreating(ModelBuilder modelBuilder) diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContextOptions.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContextOptions.cs index acde97340b..a179bb3ee8 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContextOptions.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContextOptions.cs @@ -26,8 +26,12 @@ public class AbpDbContextOptions internal Dictionary> ConventionActions { get; } internal Action? DefaultOnModelCreatingAction { get; set; } + + internal Action? DefaultOnConfiguringAction { get; set; } internal Dictionary> OnModelCreatingActions { get; } + + internal Dictionary> OnConfiguringActions { get; } public AbpDbContextOptions() { @@ -37,6 +41,7 @@ public class AbpDbContextOptions DbContextReplacements = new Dictionary(); ConventionActions = new Dictionary>(); OnModelCreatingActions = new Dictionary>(); + OnConfiguringActions = new Dictionary>(); } public void PreConfigure([NotNull] Action action) @@ -84,6 +89,13 @@ public class AbpDbContextOptions DefaultOnModelCreatingAction = action; } + + public void ConfigureDefaultOnConfiguring([NotNull] Action action) + { + Check.NotNull(action, nameof(action)); + + DefaultOnConfiguringAction = action; + } public void ConfigureOnModelCreating([NotNull] Action action) where TDbContext : AbpDbContext @@ -102,6 +114,24 @@ public class AbpDbContextOptions actions.Add(action); } + + public void ConfigureOnConfiguring([NotNull] Action action) + where TDbContext : AbpDbContext + { + Check.NotNull(action, nameof(action)); + + var actions = OnConfiguringActions.GetOrDefault(typeof(TDbContext)); + if (actions == null) + { + OnConfiguringActions[typeof(TDbContext)] = new List + { + new Action((dbContext, builder) => action((TDbContext)dbContext, builder)) + }; + return; + } + + actions.Add(action); + } public bool IsConfiguredDefault() {