添加没有 App.Config 的 DbProviderFactory
我在我的数据层(基于实体框架)中使用 DbProviderFactories,并且我的数据库使用 SQLite,但我不需要 App.Config 来获得以下代码:
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) { }
相关文章