I am in the process of migrating a solution from .NET framework 4.7.1 to .NET Core 2.1 and am having issues with Enterprise Library 6 (Using EnterpriseLibrary.Data.NetCore Nuget Package)
I am attempting to create a database from connection string and register it with Autofac.
The original code looks like this:
builder.Register(c => new DatabaseProviderFactory().Create(DefaultConnection) as SqlDatabase).InstancePerLifetimeScope();
Where DefaultConnection
is a string constant representing the web.config
Connection String.
In the web.config
I have:
<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data" requirePermission="true" />
</configSections>
<dataConfiguration defaultDatabase="DefaultConnection" />
<connectionStrings>
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="*****************" />
</connectionStrings>
This works fine.
In my .NET Core 2.1 solution I am using the following code
builder.Register(_ => new DatabaseProviderFactory().Create(Constants.DefaultConnection) as SqlDatabase).InstancePerLifetimeScope();
But this throws the following exception:
The connection string for the database 'DefaultConnection' does not exist or does not have a valid provider.
- The requested database DefaultConnection is not defined in configuration
I have a web.config
with the same settings in the solution, but it is not being read, I also have a appsettings.json
with the connection strings.
I have tried the following code using the appsettings.json
:
builder.Register(_ => new SqlDatabase(configuration.GetConnectionString("DefaultConnection"))).InstancePerLifetimeScope();
Which seems to work, but when I call Microsoft.Practices.EnterpriseLibrary.Data.DatabaseExtensions.ExecuteSprocAccessor(...)
I get the exception
Parameter discovery is not supported for connections using GenericDatabase.
You must specify the parameters explicitly, or configure the connection to use a type deriving from Database that supports parameter discovery.'
Does anyone have any insight as to what the correct way to do this would be?