Browse Source

Update `AbpDbContextOptions`.

pull/19241/head
maliming 3 years ago
parent
commit
98e64b896b
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 59
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs
  2. 92
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContextOptions.cs

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

@ -117,29 +117,18 @@ public abstract class AbpDbContext<TDbContext> : DbContext, IAbpEfCoreDbContext,
.Invoke(this, new object[] { modelBuilder, entityType }); .Invoke(this, new object[] { modelBuilder, entityType });
} }
if (LazyServiceProvider is null) var abpDbContextOptions = LazyServiceProvider?.LazyGetRequiredService<IOptions<AbpDbContextOptions>>().Value;
if (abpDbContextOptions != null)
{ {
return; abpDbContextOptions.DefaultOnModelCreatingAction?.Invoke(this, modelBuilder);
}
var abpDbContextOptions = LazyServiceProvider.LazyGetRequiredService<IOptions<AbpDbContextOptions>>().Value;
var actions = abpDbContextOptions.ModelBuilderActions var onModelCreatingActions = abpDbContextOptions.OnModelCreatingActions.GetOrDefault(typeof(TDbContext));
.Where(x => x.Key == typeof(TDbContext) || x.Key == typeof(AbpDbContext<>)) if (!onModelCreatingActions.IsNullOrEmpty())
.SelectMany(x => x.Value)
.OrderBy(a => a.Key)
.Select(a => a.Value)
.ToList();
foreach (var action in actions)
{
if(action is Action<ModelBuilder, DbContext> modelBuilderAction)
{ {
modelBuilderAction.Invoke(modelBuilder, this); foreach (var onModelCreatingAction in onModelCreatingActions!)
} {
onModelCreatingAction.As<Action<DbContext, ModelBuilder>>().Invoke(this, modelBuilder);
if(this is TDbContext dbContext && action is Action<ModelBuilder, TDbContext> dbContextAction) }
{
dbContextAction.Invoke(modelBuilder, dbContext);
} }
} }
} }
@ -148,30 +137,18 @@ public abstract class AbpDbContext<TDbContext> : DbContext, IAbpEfCoreDbContext,
{ {
base.ConfigureConventions(configurationBuilder); base.ConfigureConventions(configurationBuilder);
if (LazyServiceProvider is null) var abpDbContextOptions = LazyServiceProvider?.LazyGetRequiredService<IOptions<AbpDbContextOptions>>().Value;
if (abpDbContextOptions != null)
{ {
return; abpDbContextOptions.DefaultConventionAction?.Invoke(this, configurationBuilder);
}
var abpDbContextOptions = LazyServiceProvider.LazyGetRequiredService<IOptions<AbpDbContextOptions>>().Value;
var actions = abpDbContextOptions.Conventions var conventionActions = abpDbContextOptions.ConventionActions.GetOrDefault(typeof(TDbContext));
.Where(x => x.Key == typeof(TDbContext) || x.Key == typeof(AbpDbContext<>)) if (!conventionActions.IsNullOrEmpty())
.SelectMany(x => x.Value)
.OrderBy(a => a.Key)
.Select(a => a.Value)
.ToList();
foreach (var action in actions)
{
if(action is Action<ModelConfigurationBuilder, DbContext> modelBuilderAction)
{ {
modelBuilderAction.Invoke(configurationBuilder, this); foreach (var conventionAction in conventionActions!)
} {
conventionAction.As<Action<DbContext, ModelConfigurationBuilder>>().Invoke(this, configurationBuilder);
if(this is TDbContext dbContext && action is Action<ModelConfigurationBuilder, TDbContext> dbContextAction) }
{
dbContextAction.Invoke(configurationBuilder, dbContext);
} }
} }
} }

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

@ -21,9 +21,13 @@ public class AbpDbContextOptions
internal Dictionary<MultiTenantDbContextType, Type> DbContextReplacements { get; } internal Dictionary<MultiTenantDbContextType, Type> DbContextReplacements { get; }
internal Dictionary<Type, List<KeyValuePair<int?, object>>> Conventions { get; } internal Action<DbContext, ModelConfigurationBuilder>? DefaultConventionAction { get; set; }
internal Dictionary<Type, List<KeyValuePair<int?, object>>> ModelBuilderActions { get; } internal Dictionary<Type, List<object>> ConventionActions { get; }
internal Action<DbContext, ModelBuilder>? DefaultOnModelCreatingAction { get; set; }
internal Dictionary<Type, List<object>> OnModelCreatingActions { get; }
public AbpDbContextOptions() public AbpDbContextOptions()
{ {
@ -31,8 +35,8 @@ public class AbpDbContextOptions
PreConfigureActions = new Dictionary<Type, List<object>>(); PreConfigureActions = new Dictionary<Type, List<object>>();
ConfigureActions = new Dictionary<Type, object>(); ConfigureActions = new Dictionary<Type, object>();
DbContextReplacements = new Dictionary<MultiTenantDbContextType, Type>(); DbContextReplacements = new Dictionary<MultiTenantDbContextType, Type>();
Conventions = new Dictionary<Type, List<KeyValuePair<int?, object>>>(); ConventionActions = new Dictionary<Type, List<object>>();
ModelBuilderActions = new Dictionary<Type, List<KeyValuePair<int?, object>>>(); OnModelCreatingActions = new Dictionary<Type, List<object>>();
} }
public void PreConfigure([NotNull] Action<AbpDbContextConfigurationContext> action) public void PreConfigure([NotNull] Action<AbpDbContextConfigurationContext> action)
@ -48,27 +52,55 @@ public class AbpDbContextOptions
DefaultConfigureAction = action; DefaultConfigureAction = action;
} }
public void ConfigureConventions([NotNull] Action<ModelConfigurationBuilder, DbContext> action, Type? dbContextType = null, int? order = null) public void ConfigureDefaultConvention([NotNull] Action<DbContext, ModelConfigurationBuilder> action)
{ {
InternalConfigureConventions(action, dbContextType, order); Check.NotNull(action, nameof(action));
DefaultConventionAction = action;
} }
public void ConfigureConventions<TDbContext>([NotNull] Action<ModelConfigurationBuilder, TDbContext> action, int? order = null) public void ConfigureConventions<TDbContext>([NotNull] Action<TDbContext, ModelConfigurationBuilder> action)
where TDbContext : AbpDbContext<TDbContext> where TDbContext : AbpDbContext<TDbContext>
{ {
InternalConfigureConventions(action, typeof(TDbContext), order); Check.NotNull(action, nameof(action));
var actions = ConventionActions.GetOrDefault(typeof(TDbContext));
if (actions == null)
{
ConventionActions[typeof(TDbContext)] = new List<object>
{
new Action<DbContext, ModelConfigurationBuilder>((dbContext, builder) => action((TDbContext)dbContext, builder))
};
return;
}
actions.Add(action);
} }
public void OnModelCreating([NotNull] Action<ModelBuilder, DbContext> action, Type? dbContextType = null, int? order = null) public void ConfigureDefaultOnModelCreating([NotNull] Action<DbContext, ModelBuilder> action)
{ {
InternalOnModelCreating(action, dbContextType, order); Check.NotNull(action, nameof(action));
DefaultOnModelCreatingAction = action;
} }
public void OnModelCreating<TDbContext>([NotNull] Action<ModelBuilder, TDbContext> action, int? order = null) public void ConfigureOnModelCreating<TDbContext>([NotNull] Action<TDbContext, ModelBuilder> action)
where TDbContext : AbpDbContext<TDbContext> where TDbContext : AbpDbContext<TDbContext>
{ {
InternalOnModelCreating(action, typeof(TDbContext), order); Check.NotNull(action, nameof(action));
var actions = OnModelCreatingActions.GetOrDefault(typeof(TDbContext));
if (actions == null)
{
OnModelCreatingActions[typeof(TDbContext)] = new List<object>
{
new Action<DbContext, ModelBuilder>((dbContext, builder) => action((TDbContext)dbContext, builder))
};
return;
}
actions.Add(action);
} }
public bool IsConfiguredDefault() public bool IsConfiguredDefault()
@ -131,30 +163,4 @@ public class AbpDbContextOptions
} }
} }
} }
}
private void InternalConfigureConventions(object action, Type? dbContextType = null, int? order = null)
{
Check.NotNull(action, nameof(action));
var actions = Conventions.GetOrDefault(dbContextType ?? typeof(AbpDbContext<>));
if (actions == null)
{
Conventions[dbContextType ?? typeof(AbpDbContext<>)] = actions = new List<KeyValuePair<int?, object>>();
}
actions.Add(new KeyValuePair<int?, object>(order, action));
}
private void InternalOnModelCreating(object action, Type? dbContextType = null, int? order = null)
{
Check.NotNull(action, nameof(action));
var actions = ModelBuilderActions.GetOrDefault(dbContextType ?? typeof(AbpDbContext<>));
if (actions == null)
{
ModelBuilderActions[dbContextType ?? typeof(AbpDbContext<>)] = actions = new List<KeyValuePair<int?, object>>();
}
actions.Add(new KeyValuePair<int?, object>(order, action));
}
}

Loading…
Cancel
Save