public static DbConnection GetStoreConnection(string entityConnectionString) { // Build the initial connection string. var builder = new EntityConnectionStringBuilder(entityConnectionString); // If the initial connection string refers to an entry in the configuration, load that as the builder. object configName; if (builder.TryGetValue("name", out configName)) { // As of EF 4.1, it appears that TryGetValue("name") returns a blank // string if there is no name key. Added test to confirm that // something has been returned. if (!String.IsNullOrEmpty(configName.ToString())) { var configEntry = WebConfigurationManager.ConnectionStrings[configName.ToString()]; builder = new EntityConnectionStringBuilder(configEntry.ConnectionString); } } // Find the proper factory for the underlying connection. var factory = DbProviderFactories.GetFactory(builder.Provider); // Build the new connection. DbConnection tempConnection = null; try { tempConnection = factory.CreateConnection(); tempConnection.ConnectionString = builder.ProviderConnectionString; var connection = tempConnection; tempConnection = null; return(connection); } finally { // If creating of the connection failed, dispose the connection. if (tempConnection != null) { tempConnection.Dispose(); } } }
public static DbConnection GetStoreConnection(string entityConnectionString) { // Build the initial connection string. var builder = new EntityConnectionStringBuilder(entityConnectionString); // If the initial connection string refers to an entry in the configuration, load that as the builder. object configName; if (builder.TryGetValue("name", out configName)) { var configEntry = WebConfigurationManager.ConnectionStrings[configName.ToString()]; builder = new EntityConnectionStringBuilder(configEntry.ConnectionString); } // Find the proper factory for the underlying connection. var factory = DbProviderFactories.GetFactory(builder.Provider); // Build the new connection. DbConnection tempConnection = null; try { tempConnection = factory.CreateConnection(); tempConnection.ConnectionString = builder.ProviderConnectionString; var connection = tempConnection; tempConnection = null; return(connection); } finally { // If creating of the connection failed, dispose the connection. if (tempConnection != null) { tempConnection.Dispose(); } } }