EF7与SQLite

时间:2016-04-09 16:41:46

标签: sqlite entity-framework-core

我试图使用EF7处理我的SQLite数据库。 当我使用System.Data.SQLite.SQLiteConnectionStringBuilder()初始化连接字符串时,如下所示:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    var connectionStringBuilder = new SQLiteConnectionStringBuilder() { DataSource = Constants.DataDBPath };
    connectionStringBuilder.DefaultTimeout = 5000;
    connectionStringBuilder.SyncMode = SynchronizationModes.Off;
    connectionStringBuilder.JournalMode = SQLiteJournalModeEnum.Memory;
    connectionStringBuilder.PageSize = 65536;
    connectionStringBuilder.CacheSize = 16777216;
    connectionStringBuilder.FailIfMissing = false;
    connectionStringBuilder.ReadOnly = false;
    connectionStringBuilder.Version = 3;
    var connectionString = connectionStringBuilder.ToString();
    var connection = new SQLiteConnection() { ConnectionString = connectionString };
    optionsBuilder.UseSqlite(connection);
}

我收到以下错误:

  

指定的演员表无效。

命令:

  

isExists = context.Blocks.Where(w => w.Hash == hash).Any();

当我使用Microsoft.Data.Sqlite.SqliteConnectionStringBuilder()初始化连接字符串时,如下所示:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    var connectionStringBuilder = new SqliteConnectionStringBuilder() { DataSource = Constants.DataDBPath  };
    var connectionString = connectionStringBuilder.ToString();
    var connection = new SQLiteConnection() { ConnectionString = connectionString };
    optionsBuilder.UseSqlite(connection);
}

我没有收到任何错误。一切正常但更新速度很慢。 我不知道如何配置数据库以提高性能,就像我之前的连接字符串构建器一样。 当我尝试手动添加参数时,例如" synchronous = Off; pooling = True;"对于连接字符串,我得到一个例外,即参数不被识别。

请帮忙。

更新

我试过了:

using (var context = new DataDBContext())
{
    var connection = context.Database.GetDbConnection();
    connection.Open();
    var command = connection.CreateCommand();
    command.CommandText = "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF; PRAGMA count_changes=OFF; PRAGMA temp_store=OFF; PRAGMA page_size=65536; PRAGMA cache_size=-16777216;";
    command.ExecuteNonQuery();
}

但它没有帮助。

1 个答案:

答案 0 :(得分:1)

您必须将Microsoft提供的提供程序与EF Core一起使用,并且它仅支持System.Data.Sqlite中的一小部分连接字符串选项。您可以通过编译指示语句设置的许多选项