@@ -24,39 +24,40 @@ public class DbContextManager : IDbContextManager
2424 private ModelWrapper [ ] _knownModels ;
2525 private readonly ILoggerFactory _loggerFactory ;
2626 private readonly IConfigManager _configManager ;
27+ private static readonly Type [ ] AllDbContextTypes ;
28+ static DbContextManager ( )
29+ {
30+ AllDbContextTypes = ReflectionTool . GetPublicClasses ( typeof ( DbContext ) )
31+ . Where ( type => type != typeof ( DbContext ) && typeof ( DbContext ) . IsAssignableFrom ( type ) ) . ToArray ( ) ;
32+ }
2733
2834 /// <inheritdoc />
2935 public DbContextManager ( IConfigManager configManager , ILoggerFactory loggerFactory )
3036 {
3137 _loggerFactory = loggerFactory ;
3238 _configManager = configManager ;
3339
34- var dbContextTypes = ReflectionTool . GetPublicClasses (
35- typeof ( DbContext ) ,
36- type => type != typeof ( DbContext ) && ! type . GetCustomAttributes < DatabaseSpecificContextAttribute > ( ) . Any ( ) ) ;
37-
38- _knownModels = dbContextTypes
40+ var baseDbContextTypes = AllDbContextTypes
41+ . Where ( type => ! type . GetCustomAttributes < DatabaseSpecificContextAttribute > ( ) . Any ( ) ) ;
42+
43+ _knownModels = baseDbContextTypes
3944 . Select ( dbContextType =>
4045 {
4146 var config = configManager . GetConfiguration < DatabaseConfig < DatabaseConnectionSettings > > ( ConfigFilename ( dbContextType ) ) ;
42-
4347 var configuratorType = ! string . IsNullOrEmpty ( config . ConfiguratorTypename )
4448 ? Type . GetType ( config . ConfiguratorTypename )
4549 : DefaultConfigurator ( ) ;
4650
47- return new
51+ // Try to find specific DbContext for the configurator
52+ // If no specific context found, use the base one
53+ var specificDbContext = GetSpecificDbContext ( dbContextType , configuratorType ) ;
54+
55+ return new ModelWrapper
4856 {
4957 DbContextType = dbContextType ,
50- ConfiguratorType = configuratorType ,
58+ SpecificDbContext = specificDbContext ,
59+ Configurator = ( IModelConfigurator ) Activator . CreateInstance ( configuratorType )
5160 } ;
52- } ) . Select ( t =>
53- {
54- var wrapper = new ModelWrapper
55- {
56- DbContextType = t . DbContextType ,
57- Configurator = ( IModelConfigurator ) Activator . CreateInstance ( t . ConfiguratorType )
58- } ;
59- return wrapper ;
6061 } ) . ToArray ( ) ;
6162
6263 foreach ( var wrapper in _knownModels )
@@ -65,6 +66,7 @@ public DbContextManager(IConfigManager configManager, ILoggerFactory loggerFacto
6566 }
6667 }
6768
69+ // TODO: Reference to an assembly which might not be referencesd in certain setups
6870 private Type DefaultConfigurator ( )
6971 {
7072 var sqliteModelConfigurator = ReflectionTool . GetAssemblies ( )
@@ -82,15 +84,31 @@ public void UpdateConfig(Type dbContextType, Type configuratorType, IDatabaseCon
8284
8385 var modelWrapper = _knownModels . First ( w => w . DbContextType == dbContextType ) ;
8486 modelWrapper . Configurator = ( IModelConfigurator ) Activator . CreateInstance ( configuratorType ) ;
85-
87+ modelWrapper . SpecificDbContext = GetSpecificDbContext ( dbContextType , configuratorType ) ;
88+
8689 InitializeConfigurator ( modelWrapper ) ;
8790 }
8891
92+ private static Type GetSpecificDbContext ( Type dbContextType , Type configuratorType )
93+ {
94+ return AllDbContextTypes . FirstOrDefault ( type =>
95+ {
96+ if ( ! dbContextType . IsAssignableFrom ( type ) )
97+ return false ;
98+
99+ var modelConfiguratorAttr = type . GetCustomAttribute < ModelConfiguratorAttribute > ( ) ;
100+ if ( modelConfiguratorAttr == null )
101+ return false ;
102+
103+ return modelConfiguratorAttr . ConfiguratorType == configuratorType ;
104+ } ) ?? dbContextType ;
105+ }
106+
89107 private void InitializeConfigurator ( ModelWrapper modelWrapper )
90108 {
91109 var configuratorType = modelWrapper . Configurator . GetType ( ) ;
92110 var logger = _loggerFactory . CreateLogger ( configuratorType ) ;
93- modelWrapper . Configurator . Initialize ( modelWrapper . DbContextType , _configManager , logger ) ;
111+ modelWrapper . Configurator . Initialize ( modelWrapper . SpecificDbContext , _configManager , logger ) ;
94112 }
95113
96114 private string ConfigFilename ( Type dbContextType )
@@ -132,6 +150,8 @@ private class ModelWrapper
132150 public Type DbContextType { get ; set ; }
133151
134152 public IModelConfigurator Configurator { get ; set ; }
153+
154+ public Type SpecificDbContext { get ; set ; }
135155 }
136156 }
137157}
0 commit comments