Browse Source

Allow extending ConfigureConventions and OnModelCreating

pull/19236/head
Salih 3 years ago
parent
commit
c1d49e47b3
  1. 19
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs
  2. 66
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContextOptions.cs

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

@ -118,10 +118,14 @@ public abstract class AbpDbContext<TDbContext> : DbContext, IAbpEfCoreDbContext,
}
var abpDbContextOptions = LazyServiceProvider.LazyGetRequiredService<IOptions<AbpDbContextOptions>>().Value;
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();
var modelBuilderActions = abpDbContextOptions.ModelBuilderActions.Where(x => x.Key == typeof(TDbContext) || x.Key == typeof(AbpDbContext<>)).SelectMany(x => x.Value).ToList();
var actions = modelBuilderActions.OrderBy(a => a.Key).Select(a => a.Value).ToList();
foreach (var action in actions)
{
if(action is Action<ModelBuilder, DbContext> modelBuilderAction)
@ -142,9 +146,12 @@ public abstract class AbpDbContext<TDbContext> : DbContext, IAbpEfCoreDbContext,
var abpDbContextOptions = LazyServiceProvider.LazyGetRequiredService<IOptions<AbpDbContextOptions>>().Value;
var conventions = abpDbContextOptions.Conventions.Where(x => x.Key == typeof(TDbContext) || x.Key == typeof(AbpDbContext<>)).SelectMany(x => x.Value).ToList();
var actions = conventions.OrderBy(a => a.Key).Select(a => a.Value).ToList();
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)
{

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

@ -51,56 +51,24 @@ public class AbpDbContextOptions
public void ConfigureConventions([NotNull] Action<ModelConfigurationBuilder, DbContext> 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));
InternalConfigureConventions(action, dbContextType, order);
}
public void ConfigureConventions<TDbContext>([NotNull] Action<ModelConfigurationBuilder, TDbContext> action, int? order = null)
where TDbContext : AbpDbContext<TDbContext>
{
Check.NotNull(action, nameof(action));
var actions = Conventions.GetOrDefault(typeof(TDbContext));
if (actions == null)
{
Conventions[typeof(TDbContext)] = actions = new List<KeyValuePair<int?, object>>();
}
actions.Add(new KeyValuePair<int?, object>(order, action));
InternalConfigureConventions(action, typeof(TDbContext), order);
}
public void OnModelCreating([NotNull] Action<ModelBuilder, DbContext> 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));
InternalOnModelCreating(action, dbContextType, order);
}
public void OnModelCreating<TDbContext>([NotNull] Action<ModelBuilder, TDbContext> action, int? order = null)
where TDbContext : AbpDbContext<TDbContext>
{
Check.NotNull(action, nameof(action));
var actions = ModelBuilderActions.GetOrDefault(typeof(TDbContext));
if (actions == null)
{
ModelBuilderActions[typeof(TDbContext)] = actions = new List<KeyValuePair<int?, object>>();
}
actions.Add(new KeyValuePair<int?, object>(order, action));
InternalOnModelCreating(action, typeof(TDbContext), order);
}
public bool IsConfiguredDefault()
@ -163,4 +131,30 @@ 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