1:20 PM 0 0
Create a DBContext

Create a DBContext

  Sergi |  July 122021
You must be create a DBContext inherit form StoreContext using a StoreSet to represents a tables. Similar idea from EntityFramework, but in our case the CURD actions, or any action to add, update or delete is directly executed into a indexedDb so no need save changes.

public class DBContext : StoreContext
{
#region properties
public StoreSet<[data model]> PlaysList { get; set; }
#endregion

#region constructor
public DBContext(IJSRuntime js) : base(js, new Settings { DBName = "[database name]", Version = [number od version] }) { }
#endregion

}

Initialize the DBContext

Must be injected in the service dependences the DBContext for can use more easy. In the file Program.cs add the line in bold on this example code.
using BlazorIndexedDb;
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add< App >("#app");
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

builder.AddBlazorIndexedDbContext<[DBContext]>();

var app = builder.Build();
await app.UseBlazorIndexedDbContext<[DBContext]>();
await app.RunAsync();
}
}

0 Guest reviews

 
 
 

File