private static DbContextInitializerBase CreateInitializer(DbContextInitializerConfig config) { Type initializerType = config.InitializerType; DbContextInitializerBase initializer = Activator.CreateInstance(initializerType) as DbContextInitializerBase; if (initializer == null) { throw new InvalidOperationException(Resources.DatabaseInitializer_TypeNotDatabaseInitializer.FormatWith(initializerType)); } foreach (Assembly mapperAssembly in config.EntityMapperAssemblies) { if (initializer.MapperAssemblies.Contains(mapperAssembly)) { continue; } initializer.MapperAssemblies.Add(mapperAssembly); } dynamic dynamicInitializer = initializer; if (config.CreateDatabaseInitializerType != null) { dynamic createDatabaseInitializer = Activator.CreateInstance(config.CreateDatabaseInitializerType); dynamicInitializer.CreateDatabaseInitializer = createDatabaseInitializer; } return((DbContextInitializerBase)dynamicInitializer); }
/// <summary> /// 注册上下文初始化器 /// </summary> /// <param name="contextType">上下文类型</param> /// <param name="initializer">上下文初始化器</param> public void RegisterInitializer(Type contextType, DbContextInitializerBase initializer) { if (_contextInitializerDict.ContainsKey(contextType)) { return; } _contextInitializerDict[contextType] = initializer; initializer.Initialize(); }
/// <summary> /// 初始化数据上下文 /// </summary> /// <param name="config">数据上下文配置信息</param> private static void DbContextInit(DbContextConfig config) { if (!config.Enabled) { return; } DbContextInitializerBase initializer = CreateInitializer(config.InitializerConfig); DbContextManager.Instance.RegisterInitializer(config.ContextType, initializer); }