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

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<Type, List<KeyValuePair<int?, object>>> Conventions { get; }
internal Dictionary<Type, List<KeyValuePair<int?, object>>> ModelBuilderActions { get; }
internal Action<DbContext, ModelConfigurationBuilder>? DefaultConventionAction { get; set; }
internal Dictionary<Type, List<object>> ConventionActions { get; }
internal Action<DbContext, ModelBuilder>? DefaultOnModelCreatingAction { get; set; }
internal Dictionary<Type, List<object>> OnModelCreatingActions { get; }
public AbpDbContextOptions()
{
@ -31,8 +35,8 @@ public class AbpDbContextOptions
PreConfigureActions = new Dictionary<Type, List<object>>();
ConfigureActions = new Dictionary<Type, object>();
DbContextReplacements = new Dictionary<MultiTenantDbContextType, Type>();
Conventions = new Dictionary<Type, List<KeyValuePair<int?, object>>>();
ModelBuilderActions = new Dictionary<Type, List<KeyValuePair<int?, object>>>();
ConventionActions = new Dictionary<Type, List<object>>();
OnModelCreatingActions = new Dictionary<Type, List<object>>();
}
public void PreConfigure([NotNull] Action<AbpDbContextConfigurationContext> action)
@ -48,27 +52,55 @@ public class AbpDbContextOptions
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>
{
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>
{
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()
@ -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