Dot-Net

SQLite EF6 在執行時以程式方式設置連接字元串

  • February 28, 2014

我嘗試從 EF 3.5 遷移到 6(使用 SQLite 作為數據庫)。我們無法在應用程序配置文件中設置連接字元串(這對 ef6 沒有問題)。我們必須在執行時以程式方式設置連接字元串(在使用者選擇 SQLite 文件之後)。

這是我們的 app.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <configSections>
   <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
   <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
 </configSections>
 <connectionStrings>
   <add name="testContext" connectionString="data source=Data\testdb.sqlite;Foreign Keys=True"
     providerName="System.Data.SQLite" />
 </connectionStrings>
 <entityFramework>
   <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
     <parameters>
       <parameter value="v11.0" />
     </parameters>
   </defaultConnectionFactory>
   <providers>
     <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
   </providers>
 </entityFramework>
 <system.data>
   <DbProviderFactories>
     <remove invariant="System.Data.SQLite" />
     <remove invariant="System.Data.SQLite.EF6" />
     <add name="System.Data.SQLite" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
   </DbProviderFactories>
 </system.data>
</configuration>

這是 DbContext

public class FirmwareContext : DbContext
{
   public FirmwareContext()
       : this(DEFAULT_CONNECTION)
   {

   }

   public FirmwareContext(string connextionNameOrString)
       : base(connextionNameOrString)
   {

   }
}

如果我使用 app.config 中的連接名稱進行連接,則一切正常。如果我嘗試使用第二個建構子傳遞連接字元串,則會失敗

這是一個小例子

SQLiteConnectionStringBuilder builder =
   factory.CreateConnectionStringBuilder() as SQLiteConnectionStringBuilder;
builder.DataSource = dataSource; // Path to file name of the user
builder.ForeignKeys = true;

var context = new FirmwareContext(builder.ToString());

var context = new FirmwareContext(builder.ToString());
var test = context.Firmware.FirstOrDefault();

我得到以下異常(“Schlüsselwort wird nicht unterstützt:‘外鍵’。”)=>不支持鍵’外鍵’。如果我刪除外鍵集,我會得到以下異常(“提供程序沒有返回 ProviderManifestToken 字元串。”)和一些內部異常。

似乎 bas(connectionString) 為 SQLite 建構了 MSSQL 連接字元串。

如何使我的第二個建構子與 sqlite 兼容?

我遇到了同樣的問題。我通過使用與基礎 DbContext 類不同的建構子找到了一種解決方法:

public DbContext(DbConnection existingConnection, bool contextOwnsConnection);

使用此覆蓋,您可以傳遞一個 SQLiteConnection 來代替您設置連接字元串。因此,例如,您可以向 FirmwareContext 添加一個新的建構子。

public FirmwareContext(string connectionString)
   : base(new SQLiteConnection() { ConnectionString = connectionString }, true)
{
}

甚至

public FirmwareContext(string filename)
   : base(new SQLiteConnection() { ConnectionString =
           new SQLiteConnectionStringBuilder()
               { DataSource = filename, ForeignKeys = true }
           .ConnectionString }, true)
{
}

引用自:https://stackoverflow.com/questions/22101150