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. + }); }); ```` 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..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,6 +117,17 @@ 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)) ?? []) + { + onConfiguringAction.As>().Invoke(this, optionsBuilder); + } } protected override void OnModelCreating(ModelBuilder modelBuilder) @@ -798,7 +809,7 @@ public abstract class AbpDbContext : DbContext, IAbpEfCoreDbContext, modelBuilder, mutableEntityType ); - + entityTypeBuilder.ConfigureByConvention(); ConfigureGlobalFilters(modelBuilder, mutableEntityType, entityTypeBuilder); @@ -815,7 +826,7 @@ public abstract class AbpDbContext : DbContext, IAbpEfCoreDbContext, protected virtual void ConfigureGlobalFilters( ModelBuilder modelBuilder, - IMutableEntityType mutableEntityType, + IMutableEntityType mutableEntityType, EntityTypeBuilder entityTypeBuilder) where TEntity : class { @@ -846,7 +857,7 @@ public abstract class AbpDbContext : DbContext, IAbpEfCoreDbContext, { return; } - + foreach (var property in mutableEntityType.GetProperties(). Where(property => property.PropertyInfo != null && @@ -858,7 +869,7 @@ public abstract class AbpDbContext : DbContext, IAbpEfCoreDbContext, modelBuilder, mutableEntityType ); - + entityTypeBuilder .Property(property.Name) .HasConversion(property.ClrType == typeof(DateTime) @@ -868,7 +879,7 @@ public abstract class AbpDbContext : DbContext, IAbpEfCoreDbContext, } protected virtual void ConfigureValueGenerated( - ModelBuilder modelBuilder, + ModelBuilder modelBuilder, IMutableEntityType mutableEntityType) where TEntity : class { 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() {