From 6255631b8b80c1ba6d274f8e62e9e6230523e7cb 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 1/3] 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 c661799837..b1d1641a1d 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() { From cccc92b9281f638ab701dae81bcf93a1fca1a64a Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 27 Oct 2025 21:11:59 +0800 Subject: [PATCH 2/3] Document OnConfiguring options in EF Core setup --- docs/en/framework/data/entity-framework-core/index.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/docs/en/framework/data/entity-framework-core/index.md b/docs/en/framework/data/entity-framework-core/index.md index 208baeefda..418c040565 100644 --- a/docs/en/framework/data/entity-framework-core/index.md +++ b/docs/en/framework/data/entity-framework-core/index.md @@ -146,7 +146,7 @@ Configure(options => }); ```` -Add actions for the `ConfigureConventions` and `OnModelCreating` methods of the `DbContext` as shown below: +Add actions for the `ConfigureConventions`, `OnModelCreating` and `OnConfiguring` methods of the `DbContext` as shown below: ````csharp Configure(options => @@ -170,6 +170,15 @@ Configure(options => { // This action is called for OnModelCreating method of specific DbContext. }); + + options.ConfigureDefaultOnConfiguring((dbContext, optionsBuilder) => + { + // This action is called for OnConfiguring method of all DbContexts. + }); + options.ConfigureOnConfiguring((dbContext, optionsBuilder) => + { + // This action is called for OnConfiguring method of specific DbContext. + }); }); ```` From 4ba1cc857e39474ff2639edd6719095f30930854 Mon Sep 17 00:00:00 2001 From: maliming Date: Tue, 28 Oct 2025 14:10:56 +0800 Subject: [PATCH 3/3] Add null checks in OnConfiguring of AbpDbContext --- .../Abp/EntityFrameworkCore/AbpDbContext.cs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) 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 b1d1641a1d..45fe447e88 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs @@ -117,7 +117,12 @@ public abstract class AbpDbContext : DbContext, IAbpEfCoreDbContext, { optionsBuilder.ConfigureWarnings(c => c.Ignore(RelationalEventId.PendingModelChangesWarning)); base.OnConfiguring(optionsBuilder); - + + if (LazyServiceProvider == null || Options == null) + { + return; + } + Options.Value.DefaultOnConfiguringAction?.Invoke(this, optionsBuilder); foreach (var onConfiguringAction in Options.Value.OnConfiguringActions.GetOrDefault(typeof(TDbContext)) ?? []) { @@ -804,7 +809,7 @@ public abstract class AbpDbContext : DbContext, IAbpEfCoreDbContext, modelBuilder, mutableEntityType ); - + entityTypeBuilder.ConfigureByConvention(); ConfigureGlobalFilters(modelBuilder, mutableEntityType, entityTypeBuilder); @@ -821,7 +826,7 @@ public abstract class AbpDbContext : DbContext, IAbpEfCoreDbContext, protected virtual void ConfigureGlobalFilters( ModelBuilder modelBuilder, - IMutableEntityType mutableEntityType, + IMutableEntityType mutableEntityType, EntityTypeBuilder entityTypeBuilder) where TEntity : class { @@ -852,7 +857,7 @@ public abstract class AbpDbContext : DbContext, IAbpEfCoreDbContext, { return; } - + foreach (var property in mutableEntityType.GetProperties(). Where(property => property.PropertyInfo != null && @@ -864,7 +869,7 @@ public abstract class AbpDbContext : DbContext, IAbpEfCoreDbContext, modelBuilder, mutableEntityType ); - + entityTypeBuilder .Property(property.Name) .HasConversion(property.ClrType == typeof(DateTime) @@ -874,7 +879,7 @@ public abstract class AbpDbContext : DbContext, IAbpEfCoreDbContext, } protected virtual void ConfigureValueGenerated( - ModelBuilder modelBuilder, + ModelBuilder modelBuilder, IMutableEntityType mutableEntityType) where TEntity : class {