JeremyLikness / SqliteWasmHelper

Persistent SQLite in Blazor WebAssembly apps with EF Core 6.0 and your browser's cache.
https://youtu.be/ZeJISZgy-FM
MIT License
116 stars 24 forks source link

Question: How to use a restore existing database #39

Open singhvivek2503 opened 2 months ago

singhvivek2503 commented 2 months ago

I have a existing sqlite db file and want to restore that database. I did check this PR but unable to figure out how to use it in my existing blazor project? @Eddie-Hartman @JeremyLikness

Eddie-Hartman commented 2 months ago

@singhvivek2503 see here: #30 @JeremyLikness seems to no longer be maintaining the repo.

If you need the functionality, you can download the code itself and reference the project directly instead of using the NuGet package. That's what I currently do for my projects.

singhvivek2503 commented 2 months ago

@singhvivek2503 see here: #30 @JeremyLikness seems to no longer be maintaining the repo.

If you need the functionality, you can download the code itself and reference the project directly instead of using the NuGet package. That's what I currently do for my projects.

Thanks for responding! have added a project reference. Still struggling with steps to follow to restore the db file saved in my project folder. Could you please help?

Eddie-Hartman commented 2 months ago

In my case, I have an upload file section where I upload the specific file I want to restore (for debugging purposes generally).

I'm using MudBlazor's file upload component. This becomes an IBrowserFile.

I then convert to a stream and read that into a byte array.

Then pass that byte array into the context factory ManualRestore method.

Like so:

IBrowserFile file = new(); //your file here
Stream stream = file!.OpenReadStream(maxAllowedSize: 1024 * 1_000_000);
byte[] backupFile = new byte[file.Size];
await stream.ReadAsync(backupFile);
await YourDbContextFactory.ManualRestore(backupFile);

You can probably skip/modify the first 3 lines to just put in a byte array of your file into the ManualRestore method.

Edit: The only other thing I can think of is that you may NEED to do a forceReload nav to make it grab the new data from the db after awaiting the ManualRestore call.

NavigationManager.NavigateTo("index", true); //whatever path you want
JeremyLikness commented 2 months ago

If you know someone who is willing and able to take over maintenance, let me know and I'll sync with them.

Eddie-Hartman commented 2 months ago

@JeremyLikness I'm willing to do what I can to help keep this repo maintained.

singhvivek2503 commented 2 months ago

In my case, I have an upload file section where I upload the specific file I want to restore (for debugging purposes generally).

I'm using MudBlazor's file upload component. This becomes an IBrowserFile.

I then convert to a stream and read that into a byte array.

Then pass that byte array into the context factory ManualRestore method.

Like so:

IBrowserFile file = new(); //your file here
Stream stream = file!.OpenReadStream(maxAllowedSize: 1024 * 1_000_000);
byte[] backupFile = new byte[file.Size];
await stream.ReadAsync(backupFile);
await YourDbContextFactory.ManualRestore(backupFile);

You can probably skip/modify the first 3 lines to just put in a byte array of your file into the ManualRestore method.

Edit: The only other thing I can think of is that you may NEED to do a forceReload nav to make it grab the new data from the db after awaiting the ManualRestore call.

NavigationManager.NavigateTo("index", true); //whatever path you want

I need to place the db file in wwwroot folder and download it using HTTP to get it working. Thank you so much!