How do you use EF Core with SQLite in a MAUI app?
Short answer
Add Microsoft.EntityFrameworkCore.Sqlite, define a DbContext pointing at a file in AppDataDirectory, and call Database.EnsureCreated or run migrations at startup.
Advertisement
AModel answer
Add Microsoft.EntityFrameworkCore.Sqlite, define a DbContext pointing at a file in AppDataDirectory, and call Database.EnsureCreated or run migrations at startup. EF Core gives LINQ queries and change tracking; be mindful of startup cost and consider registering the context appropriately for a client app.
public class AppDbContext : DbContext
{
public DbSet<Note> Notes => Set<Note>();
protected override void OnConfiguring(DbContextOptionsBuilder o) =>
o.UseSqlite($"Filename={Path.Combine(
FileSystem.AppDataDirectory, \"app.db\")}");
}Real-world example
A team reuses their server EF models on the client, querying local notes with LINQ instead of raw SQL.
Likely follow-ups
- EF Core vs sqlite-net trade-offs? | How do you apply migrations on device?
Related topics