我尝试将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'.") => 关键字“外键”不受支持。如果我删除外键设置,则会收到以下异常("The provider did not return a ProviderManifestToken string.")以及一些内部异常。
看起来bas(connectionString)构建了MSSQL连接字符串和SQLite连接字符串。
我如何使我的第二个构造函数与sqlite兼容?