【发布时间】:2014-04-01 19:24:33
【问题描述】:
我尝试从 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: 'foreign keys'.”)=> 不支持键 'foreign key'。如果我删除外键集,我会得到以下异常(“提供程序没有返回 ProviderManifestToken 字符串。”)和一些内部异常。
似乎 bas(connectionString) 为 SQLite 构建了 MSSQL 连接字符串。
如何使我的第二个构造函数与 sqlite 兼容?
【问题讨论】:
标签: .net sqlite runtime entity-framework-6 connection-string