在 SQLite 中为 Entity Framework Core 代码优先设置日志模式

Tyr*_*ess 5 c# sqlite entity-framework-core sqlite-journal-mode

我在实体框架上使用 DBContext,使用本教程中的过程来创建数据库。

public class BloggingContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite("Filename=blog.db");
    }
}
Run Code Online (Sandbox Code Playgroud)

并使用以下内容保存:

using (var context = new BloggingContext())
{
    context.Add(blog);
    await context.SaveChangesAsync();
}
Run Code Online (Sandbox Code Playgroud)

我将如何将日志模式设置为 WAL 之类的东西?

Fab*_*NET 5

EF7 的 Sqlite 提供程序仅支持一小部分关闭连接字符串选项,因此您需要手动执行一些命令:

var context = new BloggingContext();
var connection = context.Database.GetDbConnection();
connection.Open();
using (var command = connection.CreateCommand())
{
    command.Text= "PRAGMA journal_mode=WAL;";
    command.ExecuteNonQuery();
}
Run Code Online (Sandbox Code Playgroud)

您可以将其包装在您的构造函数或工厂中。

相关帖子其他一篇

  • 是否必须对每个连接都执行此操作,还是可以只执行一次? (2认同)