SQLite EF6 在运行时以编程方式设置连接字符串 [英] SQLite EF6 programmatically set connection string at runtime

查看:24
本文介绍了SQLite EF6 在运行时以编程方式设置连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从 EF 3.5 迁移到 6(使用 SQLite 作为数据库).我们无法在应用程序配置文件中设置连接字符串(这对 ef6 没有问题).我们必须在运行时以编程方式设置连接字符串(在用户选择 SQLite 文件之后).

I try to migrate form EF 3.5 to 6 (with SQLite as database). We can not set the connection string in the app config file (this works without problems with ef6). We have to set connection string programmatically at runtime (after user has selected the SQLite file).

这是我们的 app.config

Here is our 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	estdb.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

Here ist the DbContext

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

    }

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

    }
}

如果我使用 app.config 中的连接名称进行连接,则一切正常.如果我尝试使用第二个构造函数传递连接字符串,则会失败

If I connect with the connecation name from app.config all works without problems. If I try to pass the connection string with the second constructor this fails

这是一个小例子

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: 'foreign keys'.")=> 键 'foreign key' 不受支持.如果我删除外键集,我会得到以下异常(提供程序没有返回 ProviderManifestToken 字符串.")和一些内部异常.

I got the following exception ("Schlüsselwort wird nicht unterstützt: 'foreign keys'.") => The key 'foreign key' is nott supported. If I remove the foreign key set, I got the following exception ("The provider did not return a ProviderManifestToken string.") and some inner exceptions.

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

It seems that the bas(connectionString) build the MSSQL connection string and for SQLite.

如何使我的第二个构造函数与 sqlite 兼容?

How can I make my second constructor campatible with sqlite?

推荐答案

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

I was having the same problem. I found a workaround by using a different constructor from the base DbContext class:

public DbContext(DbConnection existingConnection, bool contextOwnsConnection);

使用此覆盖,您可以传递一个 SQLiteConnection 来代替您设置连接字符串.因此,例如,您可以向 FirmwareContext 添加一个新的构造函数.

Using this override you can pass an SQLiteConnection instead which you set the connection string on. So for example you can add a new constructor to your 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)
{
}

这篇关于SQLite EF6 在运行时以编程方式设置连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆