src/Shared/EntityFramework/Infrastructure/SqlConnectionFactory.cs
/// <summary>
/// Creates a connection for SQL Server based on the given database name or connection string.
/// If the given string contains an '=' character then it is treated as a full connection string,
/// otherwise it is treated as a database name only.
/// </summary>
/// <param name="nameOrConnectionString"> The database name or connection string. </param>
/// <returns> An initialized DbConnection. </returns>
Creates a connection for SQL Server based on the given database name or connection string.
If the given string contains an '=' character then it is treated as a full connection string,
otherwise it is treated as a database name only.
Creates a connection for SQL Server based on the given database name or connection string . If the given string contains an ' = ' character then it is treated as a full connection string otherwise it is treated as a database name only .
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
        [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
        public DbConnection CreateConnection(string nameOrConnectionString)
        {
            Check.NotEmpty(nameOrConnectionString, "nameOrConnectionString");
            var connectionString = nameOrConnectionString;
            if (!DbHelpers.TreatAsConnectionString(nameOrConnectionString))
            {
                if (nameOrConnectionString.EndsWith(".mdf", ignoreCase: true, culture: null))
                {
                    throw Error.SqlConnectionFactory_MdfNotSupported(nameOrConnectionString);
                }
                connectionString =
                    new SqlConnectionStringBuilder(BaseConnectionString)
                        {
                            InitialCatalog = nameOrConnectionString
                        }.ConnectionString;
            }
            DbConnection connection = null;
            try
            {
                connection = ProviderFactory("System.Data.SqlClient").CreateConnection();
                DbInterception.Dispatch.Connection.SetConnectionString(
                    connection,
                    new DbConnectionPropertyInterceptionContext<string>().WithValue(connectionString));
            }
            catch
            {
                connection = new SqlConnection();
                DbInterception.Dispatch.Connection.SetConnectionString(
                    connection,
                    new DbConnectionPropertyInterceptionContext<string>().WithValue(connectionString));
            }
            return connection;
        }
[ SuppressMessage ( " " , " " ) ] [ SuppressMessage ( " " , " " ) ] public DbConnection CreateConnection ( string nameOrConnectionString ) { Check . NotEmpty ( nameOrConnectionString , " " ) ; var connectionString = nameOrConnectionString ; if ( ! DbHelpers . TreatAsConnectionString ( nameOrConnectionString ) ) { if ( nameOrConnectionString . EndsWith ( " " , ignoreCase : true , culture : null ) ) { throw Error . SqlConnectionFactory_MdfNotSupported ( nameOrConnectionString ) ; } connectionString = new SqlConnectionStringBuilder ( BaseConnectionString ) { InitialCatalog = nameOrConnectionString } . ConnectionString ; } DbConnection connection = null ; try { connection = ProviderFactory ( " " ) . CreateConnection ( ) ; DbInterception . Dispatch . Connection . SetConnectionString ( connection , new DbConnectionPropertyInterceptionContext < string > ( ) . WithValue ( connectionString ) ) ; } catch { connection = new SqlConnection ( ) ; DbInterception . Dispatch . Connection . SetConnectionString ( connection , new DbConnectionPropertyInterceptionContext < string > ( ) . WithValue ( connectionString ) ) ; } return connection ; }
Creates a connection for SQL Server based on the given database name or connection string.
Creates a connection for SQL Server based on the given database name or connection string .
// If the "name or connection string" contains an '=' character then it is treated as a connection string. // Fallback to hard-coded type if provider didn't work
An initialized DbConnection.
An initialized DbConnection .
nameOrConnectionString
string
The database name or connection string.
The database name or connection string .