I am using DbProviderFactories in my data layer (based on Entity Framework) and am using SQLite for my database, but I don't have to have a App.Config to have the following code:
<configuration>
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SQLite"/>
<add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
</DbProviderFactories>
</system.data>
</configuration>
Instead I would like to have my data layer put that in programmatically. Anyone know a way to do this?
EDIT:
The reason for this is that I am using a IoC container to pick the data layer and some of my data layers don't need the App.Config values, or have them be hard tied to the data layer.
The following will probably cause sunspots and overthrow western civilization. It may even cause a debate about Duct Tape Programming (make it stop!), but it works (for now)
Update for EF 6.0+
You can add a
DbProviderFactory
by registering aIDbDependencyResolver
and resolving for the typeDbProviderFactory
. An example of this is below:you may have to resolve for some additional types as well depending upon what type of overriding you're needing to do and how your project is setup. Basically just start with the code above and continue to debug until you've determined all the services you need to resolve for given your specific requirements.
You can read more about EF dependency resolution at the links below:
Additionally, you can do this configuration by overriding
DbConfiguration
as described in the first link above.see the following snippet
you need to pass the ProviderName that in your case is "System.Data.SQLite".
you don't need to create the app config section. That section is created by SQLite in the machine.config after the SQLite.net provider is installed.
the whole purpose of the appconfig section is to help you get the list of configured .net providers when you call the following command:
public GetProvidersList() { DataTable table= DbProviderFactories.GetFactoryClasses(); }
Chosing the DB provider factory programmatically pretty much defeats the purpose. You might as well use the classes specific to SQLite instead of all of those interfaces, no?
JoshRivers above posted a solution for SQLite. This can in fact be used for other adapters as well- I was able to get it working for MySQL using his example. I have wrapped this into something a bit more generic. This should be run once the application starts and is for the .NET connector version 6.6.5.0 (but I imagine it is good for other versions as well.)
LATE ANSWER:
You can always directly get a factory like this:
Or use your IoC container to resolve the
DbProviderFactory
, e.g.:I prefer not to use the
DbProviderFactories.GetFactory
because of its limitation of requiring a configuration file (or a hack like in @JoshRiver's answer).All
DbProviderFactories.GetFactory
does is, it looks up the registered assembly-qualified name of the factory type using the provider name, and then it gets the value of the staticInstance
property using reflection.If you don't want to use the configuration, one of the methods above might be more convenient depending on your use case.