Setting journal mode in SQLite for Entity Framework Core code-first

4.2k Views Asked by At

I'm using DBContext on Entity Framework, using a process like in this tutorial to create the DB.

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

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite("Filename=blog.db");
    }
}

and saving using something like:

using (var context = new BloggingContext())
{
    context.Add(blog);
    await context.SaveChangesAsync();
}

How would I go about setting the Journal Mode to something like WAL?

1

There are 1 best solutions below

2
On

The Sqlite provider for EF7 support only a small subset off connection string option, therefore you will need to execute some commands manually :

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

You could wrap that in your constructor or in a factory.

Related post and other one.