Skip to content

Commit 9a94dee

Browse files
authored
Merge pull request #24054 from abpframework/salihozkara/on-configuring
Add extensibility for DbContext OnConfiguring actions
2 parents 5c42bcd + 4ba1cc8 commit 9a94dee

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

docs/en/framework/data/entity-framework-core/index.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ Configure<AbpDbContextOptions>(options =>
146146
});
147147
````
148148

149-
Add actions for the `ConfigureConventions` and `OnModelCreating` methods of the `DbContext` as shown below:
149+
Add actions for the `ConfigureConventions`, `OnModelCreating` and `OnConfiguring` methods of the `DbContext` as shown below:
150150

151151
````csharp
152152
Configure<AbpDbContextOptions>(options =>
@@ -170,6 +170,15 @@ Configure<AbpDbContextOptions>(options =>
170170
{
171171
// This action is called for OnModelCreating method of specific DbContext.
172172
});
173+
174+
options.ConfigureDefaultOnConfiguring((dbContext, optionsBuilder) =>
175+
{
176+
// This action is called for OnConfiguring method of all DbContexts.
177+
});
178+
options.ConfigureOnConfiguring<YourDbContext>((dbContext, optionsBuilder) =>
179+
{
180+
// This action is called for OnConfiguring method of specific DbContext.
181+
});
173182
});
174183
````
175184

framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,17 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
117117
{
118118
optionsBuilder.ConfigureWarnings(c => c.Ignore(RelationalEventId.PendingModelChangesWarning));
119119
base.OnConfiguring(optionsBuilder);
120+
121+
if (LazyServiceProvider == null || Options == null)
122+
{
123+
return;
124+
}
125+
126+
Options.Value.DefaultOnConfiguringAction?.Invoke(this, optionsBuilder);
127+
foreach (var onConfiguringAction in Options.Value.OnConfiguringActions.GetOrDefault(typeof(TDbContext)) ?? [])
128+
{
129+
onConfiguringAction.As<Action<DbContext, DbContextOptionsBuilder>>().Invoke(this, optionsBuilder);
130+
}
120131
}
121132

122133
protected override void OnModelCreating(ModelBuilder modelBuilder)
@@ -798,7 +809,7 @@ protected virtual void ConfigureBaseProperties<TEntity>(
798809
modelBuilder,
799810
mutableEntityType
800811
);
801-
812+
802813
entityTypeBuilder.ConfigureByConvention();
803814

804815
ConfigureGlobalFilters<TEntity>(modelBuilder, mutableEntityType, entityTypeBuilder);
@@ -815,7 +826,7 @@ protected virtual EntityTypeBuilder<TEntity> CreateEntityTypeBuilderFromMutableE
815826

816827
protected virtual void ConfigureGlobalFilters<TEntity>(
817828
ModelBuilder modelBuilder,
818-
IMutableEntityType mutableEntityType,
829+
IMutableEntityType mutableEntityType,
819830
EntityTypeBuilder<TEntity> entityTypeBuilder)
820831
where TEntity : class
821832
{
@@ -846,7 +857,7 @@ protected virtual void ConfigureValueConverter<TEntity>(
846857
{
847858
return;
848859
}
849-
860+
850861

851862
foreach (var property in mutableEntityType.GetProperties().
852863
Where(property => property.PropertyInfo != null &&
@@ -858,7 +869,7 @@ protected virtual void ConfigureValueConverter<TEntity>(
858869
modelBuilder,
859870
mutableEntityType
860871
);
861-
872+
862873
entityTypeBuilder
863874
.Property(property.Name)
864875
.HasConversion(property.ClrType == typeof(DateTime)
@@ -868,7 +879,7 @@ protected virtual void ConfigureValueConverter<TEntity>(
868879
}
869880

870881
protected virtual void ConfigureValueGenerated<TEntity>(
871-
ModelBuilder modelBuilder,
882+
ModelBuilder modelBuilder,
872883
IMutableEntityType mutableEntityType)
873884
where TEntity : class
874885
{

framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContextOptions.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ public class AbpDbContextOptions
2626
internal Dictionary<Type, List<object>> ConventionActions { get; }
2727

2828
internal Action<DbContext, ModelBuilder>? DefaultOnModelCreatingAction { get; set; }
29+
30+
internal Action<DbContext, DbContextOptionsBuilder>? DefaultOnConfiguringAction { get; set; }
2931

3032
internal Dictionary<Type, List<object>> OnModelCreatingActions { get; }
33+
34+
internal Dictionary<Type, List<object>> OnConfiguringActions { get; }
3135

3236
public AbpDbContextOptions()
3337
{
@@ -37,6 +41,7 @@ public AbpDbContextOptions()
3741
DbContextReplacements = new Dictionary<MultiTenantDbContextType, Type>();
3842
ConventionActions = new Dictionary<Type, List<object>>();
3943
OnModelCreatingActions = new Dictionary<Type, List<object>>();
44+
OnConfiguringActions = new Dictionary<Type, List<object>>();
4045
}
4146

4247
public void PreConfigure([NotNull] Action<AbpDbContextConfigurationContext> action)
@@ -84,6 +89,13 @@ public void ConfigureDefaultOnModelCreating([NotNull] Action<DbContext, ModelBui
8489

8590
DefaultOnModelCreatingAction = action;
8691
}
92+
93+
public void ConfigureDefaultOnConfiguring([NotNull] Action<DbContext, DbContextOptionsBuilder> action)
94+
{
95+
Check.NotNull(action, nameof(action));
96+
97+
DefaultOnConfiguringAction = action;
98+
}
8799

88100
public void ConfigureOnModelCreating<TDbContext>([NotNull] Action<TDbContext, ModelBuilder> action)
89101
where TDbContext : AbpDbContext<TDbContext>
@@ -102,6 +114,24 @@ public void ConfigureOnModelCreating<TDbContext>([NotNull] Action<TDbContext, Mo
102114

103115
actions.Add(action);
104116
}
117+
118+
public void ConfigureOnConfiguring<TDbContext>([NotNull] Action<TDbContext, DbContextOptionsBuilder> action)
119+
where TDbContext : AbpDbContext<TDbContext>
120+
{
121+
Check.NotNull(action, nameof(action));
122+
123+
var actions = OnConfiguringActions.GetOrDefault(typeof(TDbContext));
124+
if (actions == null)
125+
{
126+
OnConfiguringActions[typeof(TDbContext)] = new List<object>
127+
{
128+
new Action<DbContext, DbContextOptionsBuilder>((dbContext, builder) => action((TDbContext)dbContext, builder))
129+
};
130+
return;
131+
}
132+
133+
actions.Add(action);
134+
}
105135

106136
public bool IsConfiguredDefault()
107137
{

0 commit comments

Comments
 (0)