从 DbProviderFactories.GetFactory() 获取 Sqlite

Dav*_*len 5 c# sqlite dbproviderfactories

我需要让 DbProviderFactories.GetFactory() 返回 Sqlite 的提供程序。但是,我已通过 NuGet 将 Sqlite 添加到我的项目中,并且希望避免将其放入 GAC 中并更新 machine.config。Sqlite 的优点之一是无需配置。

但是,我有许多库可以从 GetFactory() 中提取连接器。

有没有办法做到这一点?

谢谢-戴夫

Jos*_*VdM 5

您只需将 app.config 文件添加到您的项目中,并添加您想要添加的不变量。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.data>
    <DbProviderFactories>      
      <remove invariant="System.Data.SQLite"></remove>
      <add name="ADO.NET Provider for SQLite"
           invariant="System.Data.SQLite" 
           description="ADO.NET Provider for SQLite " 
           type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite">
      </add>          
    </DbProviderFactories>
  </system.data>
</configuration>
Run Code Online (Sandbox Code Playgroud)

type 属性包含提供者工厂的命名空间和提供者本身的命名空间。

GetFactory方法首先查找本地 app.config 文件,然后查找 machine.config。只需确保您确实拥有对 SQLite 程序集的引用即可。