SQLite EF6以编程方式在运行时设置连接字符串

Web*_*cer 24 .net sqlite connection-string runtime entity-framework-6

我尝试将表单EF 3.5迁移到6(使用SQLite作为数据库).我们无法在app配置文件中设置连接字符串(这对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>
Run Code Online (Sandbox Code Playgroud)

这是DbContext

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

    }

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

    }
}
Run Code Online (Sandbox Code Playgroud)

如果我从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();
Run Code Online (Sandbox Code Playgroud)

我得到了以下例外("Schlüsselwortwirdnichtunterstützt:'foreign keys'.")=>不支持密钥'外键'.如果我删除外键集,我得到以下异常("提供程序没有返回ProviderManifestToken字符串.")和一些内部异常.

似乎bas(connectionString)构建了MSSQL连接字符串和SQLite.

如何让我的第二个构造函数与sqlite一起使用?

kjb*_*tel 57

我遇到了同样的问题.我通过使用基本DbContext类中的不同构造函数找到了一种解决方法:

public DbContext(DbConnection existingConnection, bool contextOwnsConnection);
Run Code Online (Sandbox Code Playgroud)

使用此覆盖,您可以传递SQLiteConnection而不是您设置连接字符串.例如,您可以向FirmwareContext添加新的构造函数.

public FirmwareContext(string connectionString)
    : base(new SQLiteConnection() { ConnectionString = connectionString }, true)
{
}
Run Code Online (Sandbox Code Playgroud)

甚至

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


kjb*_*tel 7

进一步查看后,似乎问题在于没有为 SQLite 定义 IDbConnectionFactory。所以另一种方法是定义你自己的工厂实现。

public class SQLiteConnectionFactory : IDbConnectionFactory
{
    public DbConnection CreateConnection(string connectionString)
    {
        return new SQLiteConnection(connectionString);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后将defaultConnectionFactory“app.config”中的条目替换为以下内容:

<defaultConnectionFactory type="MyDAL.SQLiteConnectionFactory, MyDAL" />
Run Code Online (Sandbox Code Playgroud)