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
{
#region properties
public StoreSet<[data model]> PlaysList { get; set; }
#endregion

#region constructor
public DBContext(IJSRuntime js)
{
Settings.EnableDebug = true;
Settings settings = new Settings("[database name]");
settings.AssemblyName = "[assemble where is store the models]";
Init(settings);
}
#endregion

#region helpers
public void ProcessErrors(List result)
{
string errors = string.Empty;
foreach (ResponseJsDb error in result)
{
errors += error.Message + "
";
}
Console.WriteLine(errors);
}
#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.
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add("#app");
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

builder.Services.AddSingleton();

await builder.Build().RunAsync();
}
}

0 Guest reviews

 
 
 

File