views:

1233

answers:

4

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.

+3  A: 

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?

Steven Sudit
A: 

See here for examples: http://msdn.microsoft.com/en-us/library/dd0w4a2z(VS.80).aspx

Theo
This does not seem to address Jason's concern.
Steven Sudit
The link has information about retrieving/access the providers in the config, not about adding new ones.
Lucas
+5  A: 

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)

        try
        {
            var dataSet = ConfigurationManager.GetSection("system.data") as System.Data.DataSet;
            dataSet.Tables[0].Rows.Add("SQLite Data Provider"
            , ".Net Framework Data Provider for SQLite"
            , "System.Data.SQLite"
            , "System.Data.SQLite.SQLiteFactory, System.Data.SQLite");
        }
        catch (System.Data.ConstraintException) { }
JoshRivers
Hehe, it works. Not pretty, but it is the only thing out there. Thanks!
JasonRShaver
A: 

Hi Jason,

I have the same problem, as i use an sqlite database in a library , i can't specify directly the provider in the .config as its a library and i dont want my user referencing my library to have to add that file...

Did you find something better than having to add it in the configuration at runtime ?

Fred
Why not use the System.Data.SQLite classes directly then?
Lucas