Browse Source

Allow extending ConfigureConventions and OnModelCreating

pull/19236/head
Salih 2 years ago
parent
commit
110180ed0f
  1. 43
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs
  2. 63
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContextOptions.cs

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

@ -14,6 +14,7 @@ using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Volo.Abp.Auditing;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
@ -115,6 +116,48 @@ public abstract class AbpDbContext<TDbContext> : DbContext, IAbpEfCoreDbContext,
.MakeGenericMethod(entityType.ClrType)
.Invoke(this, new object[] { modelBuilder, entityType });
}
var abpDbContextOptions = LazyServiceProvider.LazyGetRequiredService<IOptions<AbpDbContextOptions>>().Value;
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)
{
modelBuilderAction.Invoke(modelBuilder, this);
}
if(this is TDbContext dbContext && action is Action<ModelBuilder, TDbContext> dbContextAction)
{
dbContextAction.Invoke(modelBuilder, dbContext);
}
}
}
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
base.ConfigureConventions(configurationBuilder);
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();
foreach (var action in actions)
{
if(action is Action<ModelConfigurationBuilder, DbContext> modelBuilderAction)
{
modelBuilderAction.Invoke(configurationBuilder, this);
}
if(this is TDbContext dbContext && action is Action<ModelConfigurationBuilder, TDbContext> dbContextAction)
{
dbContextAction.Invoke(configurationBuilder, dbContext);
}
}
}
protected virtual void TrySetDatabaseProvider(ModelBuilder modelBuilder)

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

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EntityFrameworkCore.DependencyInjection;
using Volo.Abp.MultiTenancy;
@ -20,12 +21,18 @@ 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; }
public AbpDbContextOptions()
{
DefaultPreConfigureActions = new List<Action<AbpDbContextConfigurationContext>>();
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>>>();
}
public void PreConfigure([NotNull] Action<AbpDbContextConfigurationContext> action)
@ -41,6 +48,60 @@ public class AbpDbContextOptions
DefaultConfigureAction = action;
}
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));
}
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));
}
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));
}
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));
}
public bool IsConfiguredDefault()
{
@ -102,4 +163,4 @@ public class AbpDbContextOptions
}
}
}
}
}
Loading…
Cancel
Save