public virtual DatabaseInitializationResult Initialize(string connectionName)
        {
            if (_ignoreConnectionNames.Contains(connectionName))
            {
                DatabaseInitializationResult result = new DatabaseInitializationResult(null, Args.Exception <Exception>("connection explicitly ignored: {0}", connectionName));
                return(result);
            }

            try
            {
                if (string.IsNullOrEmpty(connectionName))
                {
                    throw new ArgumentNullException("connectionName");
                }

                ConnectionStringResolveResult r = ResolveConnectionString(connectionName);

                ConnectionStringSettings conn = r.Settings;

                if (conn == null)
                {
                    throw new InvalidOperationException(string.Format("The connection name ({0}) was not found in the config file", connectionName));
                }

                Type factoryType = ResolveFactoryType(conn);

                DbProviderFactory factory = factoryType.GetField("Instance").GetValue(null) as DbProviderFactory;
                if (factory == null)
                {
                    throw new InvalidOperationException(string.Format("Unable to find Instance field of specified DbProviderFactory ({0})", conn.ProviderName));
                }

                Database database = GetDatabase(conn, factory);

                return(new DatabaseInitializationResult(database));
            }
            catch (Exception ex)
            {
                return(new DatabaseInitializationResult(null, ex));
            }
        }
예제 #2
0
        internal void InitializeDatabase(string connectionName, Dictionary <string, Database> databases)
        {
            DatabaseInitializationResult dir = DatabaseInitializers.TryInitialize(connectionName);

            if (dir.Success)
            {
                databases.AddMissing(connectionName, dir.Database);
            }
            else
            {
                if (FallBack != null && !TriedFallback.Contains(connectionName))
                {
                    TriedFallback.Add(connectionName);
                    FallBack(connectionName, databases);
                    InitializeDatabase(connectionName, databases);
                }
                else
                {
                    throw dir.Exception;
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Tries to initialize the database for the specified connectionName using
        /// the registered DatabaseInitializers
        /// </summary>
        /// <param name="connectionName"></param>
        /// <returns></returns>
        public static DatabaseInitializationResult TryInitialize(string connectionName)
        {
            List <string> _exceptionMessages = new List <string>();

            foreach (IDatabaseInitializer di in _initializers.Values)
            {
                DatabaseInitializationResult r = di.Initialize(connectionName);
                if (r.Success)
                {
                    r.Initializer = di;
                    return(r);
                }
                else
                {
                    if (r.Exception != null)
                    {
                        _exceptionMessages.Add(r.Exception.Message);
                    }
                }
            }

            return(new DatabaseInitializationResult(null, new DatabaseInitializationFailedException(_exceptionMessages)));
        }