Step to implement

Install/ Add Microsoft.EntityFrameworkCore.Sqlite to Project

1
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.7" />

Register App db Context before var app = builder.Build(); in MauiProgram.cs

1
2
3
var dbPath = Path.Combine(FileSystem.AppDataDirectory, "waterreminder.db3");

builder.Services.AddDbContext<AppDbContext>(options => options.UseSqlite($"Filename={dbPath}"));

Just Before app run add this Command

1
2
3
4
5
6
7
8
// Initialize the database 
using (var scope = app.Services.CreateScope())
{
    var dbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
    dbContext.Database.EnsureCreated();
}

return app;

Create Add Db Context

1
2
3
4
5
6
namespace WaterReminder.Data;

public class AppDbContext(DbContextOptions<AppDbContext> options) : DbContext(options)
{
    public DbSet<Reminder> Reminders { get; set; }
}

Below is service or uses

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
public class ReminderService : IReminderService
{
    private readonly AppDbContext _context;

    public ReminderService(AppDbContext context)
    {
        _context = context; 
    }

    public Task<List<Reminder>> GetRemindersAsync() => _context.Reminders.ToListAsync();
}