Create your database class inheriting from BlazorDBContext in the BlazorDB namespace
using BlazorDB;using Microsoft.EntityFrameworkCore;namespace MyBlazorApp;publicclassPerson{publicintId{get;set;}publicstringName{get;set;}publicstringAddress{get;set;}publicPerson(){Name=string.Empty;Address=string.Empty;}}publicclassMyBlazorDatabase:BlazorDBContext{publicDbSet<Person> Persons {get;set;}// no need to override OnConfiguring, it is already good to go}
In the Main method of the Program.cs class, call the ConfigureBlazorDBAsync method
It requires the type of your database class as a generic argument
When you want to copy the database to the browser permanent storage, call the SaveToCacheAsync method
Example code
Some basic usage in a razor page : adding a Person to the database, and the data persists when your refresh the page
@page "/"
@using Microsoft.EntityFrameworkCore
<PageTitle>Index</PageTitle>
<h1>Hello, world!</h1>
Welcome to your new app.
<SurveyPrompt Title="How is Blazor working for you?" />
<button @onclick="AddNewPerson">Add new person</button>
<ul>
@foreach (Person p in People)
{
<li>Id : @p.Id - @p.Name</li>
}
</ul>
@code {privateList<Person>People=newList<Person>();privatestring[]names={"Ben","Johnny","Rob","Ryan","James","Bob","Carlos"};protectedoverrideasync Task OnInitializedAsync(){usingMyBlazorDatabasedb=new MyBlazorDatabase();People=await db.Persons.ToListAsync();}/// <summary>/// Adds a new person to the database, and then saves it to the cache storage/// </summary>/// <returns></returns>privateasync Task AddNewPerson(){usingMyBlazorDatabasedb=new MyBlazorDatabase();Personp=new Person
{// Selects a random nameName= names[Random.Shared.Next(names.Length)],Address="No adress yet"};
db.Persons.Add(p);
People.Add(p);// Saves a copy of the DB to the permament cache storageawait db.SaveToCacheAsync();}}