添加没有 App.Config 的 DbProviderFactory

Add a DbProviderFactory without an App.Config(添加没有 App.Config 的 DbProviderFactory)
本文介绍了添加没有 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) { }

这篇关于添加没有 App.Config 的 DbProviderFactory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

FastAPI + Tortoise ORM + FastAPI Users (Python) - Relationship - Many To Many(FastAPI+Tortoise ORM+FastAPI用户(Python)-关系-多对多)
Window functions not working in pd.read_sql; Its shows error(窗口函数在pd.read_sql中不起作用;它显示错误)
(Closed) Leaflet.js: How I can Do Editing Geometry On Specific Object I Select Only?((已关闭)Leaflet.js:如何仅在我选择的特定对象上编辑几何图形?)
in sqlite update trigger with multiple if/Case Conditions(在具有多个IF/CASE条件的SQLite UPDATE触发器中)
Android: Why is Room so slow?(Android:为什么Room这么慢?)
Remote Procedure call failed with sql server 2008 R2(使用 sql server 2008 R2 的远程过程调用失败)