添加DbProviderFactory没有一个app.config [英] Add a DbProviderFactory without an App.Config
问题描述
我是用我的数据层(基于实体框架)DbProviderFactories和我使用SQLite为我的数据库,但我不必须有一个App.config中有以下code:
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?
编辑:
这样做的原因是,我使用IoC容器来接数据层和我的一些数据层并不需要在app.config值,或让他们很难连接到数据层。
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)
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) { }
这篇关于添加DbProviderFactory没有一个app.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!