Can I change the connection string if I use EnterpriseLibraryContainer.Current.GetInstance<Database>(..)

Topics: Data Access Application Block, Enterprise Library Core
Nov 10, 2014 at 8:30 PM
Edited Nov 10, 2014 at 9:58 PM

I am connecting to a database using
EnterpriseLibraryContainer.Current.GetInstance<Database>( my_database_key )
I am using EL 5. I create the database this way because it uses either Oracle.ManagedDataAccess.Client or System.Data.OracleClient (depends on the environment).

I use EntLib Contrib data extension with ODP.Net for Oracle.ManagedDataAccess.Client access.

I would like to change the connection string before the EL uses it.

I found an example of using the fluent API but I am not sure if this works.
var builder = new ConfigurationSourceBuilder();

                   .ForDatabaseNamed( my_database_key )
                     .ThatIs.AnotherDatabaseType( my_provider )
                     .WithConnectionString( my_connection_string )

            var configSource = new DictionaryConfigurationSource();
            EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
However, when I then try to instanciate other EL objects such as Exception Handling and logging using
then I get an error.

So is it possible to use fluent API for the database but the normal EL for the other objects.

Thanks for any help,
Nov 11, 2014 at 12:33 AM
The approach in the posting FLuent API configuration AND file based configuration will work for you.

The key is to get Enterprise Library to also load the file based configuration. The posted approach will allow you to specify the database configuration programmatically in code and the logging and exception handling configuration in XML.

Just make sure that the XML configuration references the same database key that you will configure programmatically.

Randy Levy
Enterprise Library support engineer
Support How-to
Nov 12, 2014 at 3:30 PM
Edited Nov 12, 2014 at 5:13 PM

I have now looked at that article and I can run the code but I don't seem to get any database access. This is because I also use this code
      <add databaseType="EntLibContrib.Data.OdpNet.OracleDatabase, EntLibContrib.Data.OdpNet, Version=5.0.505.0, Culture=neutral, PublicKeyToken=null" name="Oracle.ManagedDataAccess.Client"/>
Using Fluent API seems to remove this code. Is that true?

I can do my own providerMappings with the Fluent API as below
                .WithProviderNamed( my_provider )
                .MappedToDatabase(Type.GetType("EntLibContrib.Data.OdpNet.OracleDatabase, EntLibContrib.Data.OdpNet, Version=5.0.505.0, Culture=neutral, PublicKeyToken=null"));
but I want to access the original EL xml structure <providerMappings> if possible rather than store the type in appSettings. I can then get the databasetype from the EL xml configuration before I use the Fluent API to overwrite it.

Is it possible to get at the providerMappings objects before I do my ConfigureData() code?

I feel like I am re-writing the DAAB and all I want is to have encrypted connection strings but not encrypt the entire CONFIG file.
Nov 12, 2014 at 5:59 PM
Is it possible to get at the providerMappings objects before I do my ConfigureData() code?
Yes, you can read those in from the configuration:
DatabaseSettings dbSettings = ConfigurationManager.GetSection(DatabaseSettings.SectionName) as DatabaseSettings;
// Assumes one provider
DbProviderMapping providerMapping = dbSettings.ProviderMappings.Single();

Randy Levy
Enterprise Library support engineer
Support How-to
Marked as answer by smallmj on 11/16/2014 at 4:26 AM
Nov 12, 2014 at 11:24 PM
That works.

Just to be clear - I want to use encrypted connection strings and not encrypt the entire config file.

Is this the right way of doing it - that is, the EL does not support this already - I don't think it does as it seems to link to the <connectionstrings> element seamlessly.