Error adding multiple entries to an Enterprise Library 5 container

Topics: Data Access Application Block, Enterprise Library Core, General discussion
Jun 26, 2014 at 5:03 PM
I am trying to register multiple database in an Enterprise Library 5 container. I am able to register one but unable to register a second. Can anybody tell me what I am doing wrong in the code snippet below? If I uncomment the commented out section then an error is returned saying 'An item with the same key has already been added.'

I have checked various threads but have been unable to find an answer. https://entlib.codeplex.com/discussions/401337 http://entlib.codeplex.com/discussions/539775

What is the correct\preferred method for adding multiple registries to the container?
Private Sub RegisterDatabase()
Dim _connectionString as string = "ConnectionString"
Dim _connectionString2 as string = "ConnectionString2"

Dim builder = New ConfigurationSourceBuilder

builder.ConfigureData().ForDatabaseNamed(_databaseName). _
    ThatIs.AnOracleDatabase().WithConnectionString(_connectionString)
'####################################################################
'Code works fine with this section commented out. 
'If it's added back in, then it errors with 'An item with the same key has already been added.'
'builder.ConfigureData().ForDatabaseNamed("APPLICATION"). _
'    ThatIs.AnOracleDatabase().WithConnectionString(_connectionString2).AsDefault()
'####################################################################
Dim configSource = New DictionaryConfigurationSource
builder.UpdateConfigurationWithReplace(configSource)

Dim container As IUnityContainer
container = New UnityContainer()
container.AddNewExtension(Of EnterpriseLibraryCoreExtension)()

Dim configurator As New UnityContainerConfigurator(container)
EnterpriseLibraryContainer.ConfigureContainer(configurator, configSource)
EnterpriseLibraryContainer.Current = New UnityServiceLocator(container)
End Sub
Jun 30, 2014 at 2:11 PM
You can only add an application block to the ConfigurationSourceBuilder once (this is what ConfigureData() does). The solution is to keep configuring the same ConfigurationSourceBuilder:
    Private Sub RegisterDatabase()
        Dim _connectionString As String = "ConnectionString"
        Dim _connectionString2 As String = "ConnectionString2"

        Dim builder = New ConfigurationSourceBuilder

        builder.ConfigureData() _
        .ForDatabaseNamed(_databaseName). _
            ThatIs.AnOracleDatabase().WithConnectionString(_connectionString) _
        .ForDatabaseNamed("APPLICATION"). _
            ThatIs.AnOracleDatabase().WithConnectionString(_connectionString2).AsDefault()

        Dim configSource = New DictionaryConfigurationSource
        builder.UpdateConfigurationWithReplace(configSource)

        Dim container As IUnityContainer
        container = New UnityContainer()
        container.AddNewExtension(Of EnterpriseLibraryCoreExtension)()

        Dim configurator As New UnityContainerConfigurator(container)
        EnterpriseLibraryContainer.ConfigureContainer(configurator, configSource)
        EnterpriseLibraryContainer.Current = New UnityServiceLocator(container)

    End Sub

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Marked as answer by randylevy on 7/6/2014 at 8:36 AM
Jul 3, 2014 at 11:45 AM
Thank you Randy for clearing up my misunderstanding, that worked a treat.