src/Shared/EntityFramework/Internal/LazyInternalConnection.cs
// <summary>
// Creates the underlying <see cref="DbConnection" /> (which may actually be an <see cref="EntityConnection" />)
// if it does not already exist.
// </summary>
Creates the underlying  (which may actually be an )
if it does not already exist.
Creates the underlying ( which may actually be an ) if it does not already exist .
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;
                }
                else if (!DbHelpers.TryGetConnectionName(_nameOrConnectionString, out name)
                         || !TryInitializeFromAppConfig(name, AppConfig))
                {
                    if (name != null
                        && DbHelpers.TreatAsConnectionString(_nameOrConnectionString))
                    {
                        throw Error.DbContext_ConnectionStringNotFound(name);
                    }
                    if (DbHelpers.IsFullEFConnectionString(_nameOrConnectionString))
                    {
                        UnderlyingConnection = new EntityConnection(_nameOrConnectionString);
                    }
                    else
                    {
                        if (base.ProviderName != null)
                        {
                            CreateConnectionFromProviderName(base.ProviderName);
                        }
                        else
                        {
                            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.");
        }
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 ; } else if ( ! DbHelpers . TryGetConnectionName ( _nameOrConnectionString , out name ) || ! TryInitializeFromAppConfig ( name , AppConfig ) ) { if ( name != null && DbHelpers . TreatAsConnectionString ( _nameOrConnectionString ) ) { throw Error . DbContext_ConnectionStringNotFound ( name ) ; } if ( DbHelpers . IsFullEFConnectionString ( _nameOrConnectionString ) ) { UnderlyingConnection = new EntityConnection ( _nameOrConnectionString ) ; } else { if ( base . ProviderName != null ) { CreateConnectionFromProviderName ( base . ProviderName ) ; } else { 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 , " " ) ; }
Creates the underlying  (which may actually be an )
if it does not already exist.
Creates the underlying ( which may actually be an ) if it does not already exist .
// 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.  // 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 the name or connection string is a full EF connection string, then create an EntityConnection from it. // 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