I have a .net core library that needs a DbProviderFactory passed into a service as a dependency. It should be SQL Server for the foreseeable future, so I thought I could do something like this:
services.AddSingleton<IMyService>(di=>new MyService(SqlClientFactory.Instance));
... but I have multiple db connection strings in my appsettings.json file, and I don't see a mechanism to tell the Instance which connection to use.
How do I configure the DbProviderFactory to use the proper connection and connection string? There's no concept of a "name" for a connection or provider in the appsettings file.
The connection string settings from the config has a place a provider property. You can use it as follows given the right providers are configured locally:
Which can be called like so:
Normally if all you have is a DBConnection, you can create a helper method to retrieve the ProviderFactory:
However .NET core doesn't include reflection, so this won't work out of the box.