Example #1
0
 private void Initialize()
 {
     if (this.UnderlyingConnection != null)
     {
         return;
     }
     if (this._connectionInfo != null)
     {
         ConnectionStringSettings connectionString = this._connectionInfo.GetConnectionString(this.AppConfig);
         this.InitializeFromConnectionStringSetting(connectionString);
         this._connectionStringOrigin = DbConnectionStringOrigin.DbContextInfo;
         this._connectionStringName   = connectionString.Name;
     }
     else
     {
         string name;
         if (!DbHelpers.TryGetConnectionName(this._nameOrConnectionString, out name) || !this.TryInitializeFromAppConfig(name, this.AppConfig))
         {
             if (name != null && DbHelpers.TreatAsConnectionString(this._nameOrConnectionString))
             {
                 throw Error.DbContext_ConnectionStringNotFound((object)name);
             }
             if (DbHelpers.IsFullEFConnectionString(this._nameOrConnectionString))
             {
                 this.UnderlyingConnection = (DbConnection) new EntityConnection(this._nameOrConnectionString);
             }
             else if (base.ProviderName != null)
             {
                 this.CreateConnectionFromProviderName(base.ProviderName);
             }
             else
             {
                 this.UnderlyingConnection = DbConfiguration.DependencyResolver.GetService <IDbConnectionFactory>().CreateConnection(name ?? this._nameOrConnectionString);
                 if (this.UnderlyingConnection == null)
                 {
                     throw Error.DbContext_ConnectionFactoryReturnedNullConnection();
                 }
             }
             if (name != null)
             {
                 this._connectionStringOrigin = DbConnectionStringOrigin.Convention;
                 this._connectionStringName   = name;
             }
             else
             {
                 this._connectionStringOrigin = DbConnectionStringOrigin.UserCode;
             }
         }
     }
     this.OnConnectionInitialized();
 }
        // <summary>
        // Creates the underlying <see cref="DbConnection" /> (which may actually be an <see cref="EntityConnection" />)
        // if it does not already exist.
        // </summary>
        private void Initialize()
        {
            if (UnderlyingConnection == null)
            {
                Debug.Assert(AppConfig != null);

                string name;
                if (_connectionInfo != null)
                {
                    var connection = _connectionInfo.GetConnectionString(AppConfig);
                    InitializeFromConnectionStringSetting(connection);

                    _connectionStringOrigin = DbConnectionStringOrigin.DbContextInfo;
                    _connectionStringName   = connection.Name;
                }
                // If the name or connection string is a simple name or is in the form "name=xyz" then use
                // that name to try to load from the app/web config file.
                else if (!DbHelpers.TryGetConnectionName(_nameOrConnectionString, out name) ||
                         !TryInitializeFromAppConfig(name, AppConfig))
                {
                    // If the connection string is of the form name=, but the name was not found in the config file
                    // then always throw since we always interpret name= to mean find in the config file only.
                    if (name != null &&
                        DbHelpers.TreatAsConnectionString(_nameOrConnectionString))
                    {
                        throw Error.DbContext_ConnectionStringNotFound(name);
                    }

                    // If the name or connection string is a full EF connection string, then create an EntityConnection from it.
                    if (DbHelpers.IsFullEFConnectionString(_nameOrConnectionString))
                    {
                        UnderlyingConnection = new EntityConnection(_nameOrConnectionString);
                    }
                    else
                    {
                        if (base.ProviderName != null)
                        {
                            CreateConnectionFromProviderName(base.ProviderName);
                        }
                        else
                        {
                            // Otherwise figure out the connection factory to use (either the default,
                            // the one set in code, or one provided by DbContextInfo via the AppSettings property
                            UnderlyingConnection = DbConfiguration.DependencyResolver.GetService <IDbConnectionFactory>()
                                                   .CreateConnection(name ?? _nameOrConnectionString);

                            if (UnderlyingConnection == null)
                            {
                                throw Error.DbContext_ConnectionFactoryReturnedNullConnection();
                            }
                        }
                    }

                    if (name != null)
                    {
                        _connectionStringOrigin = DbConnectionStringOrigin.Convention;
                        _connectionStringName   = name;
                    }
                    else
                    {
                        _connectionStringOrigin = DbConnectionStringOrigin.UserCode;
                    }
                }

                OnConnectionInitialized();
            }

            Debug.Assert(UnderlyingConnection != null, "Connection should have been initialized by some mechanism.");
        }