Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 973
Tyress Avatar asked Apr 15 '16 01:04

Tyress


People also ask

Does EF core work with SQLite?

This database provider allows Entity Framework Core to be used with SQLite. The provider is maintained as part of the Entity Framework Core project.

How can create SQLite database in Entity Framework Core?

Create SQLite Database from code with EF Core MigrationsGenerate new EF Core migration files by running the command dotnet ef migrations add InitialCreate from the project root folder (where the WebApi. csproj file is located), these migrations will create the database and tables for the . NET Core API.

Does SQLite support .NET core?

To install EF Core, you install the package for the EF Core database provider(s) you want to target. This tutorial uses SQLite because it runs on all platforms that . NET Core supports.

How do I use code first in Entity Framework Core?

Code-First is mainly useful in Domain Driven Design. In the Code-First approach, you focus on the domain of your application and start creating classes for your domain entity rather than design your database first and then create the classes which match your database design.


1 Answers

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.

like image 78
Fabien ESCOFFIER Avatar answered Oct 23 '22 04:10

Fabien ESCOFFIER