3

I have a .NET Maui Blazor App and configured SQLite storage via Entity Framework Core:

builder.UseSqlite($"Data Source=app.db")

Everything is working fine. For debugging purposes, I want to access the SQLite database file from a database explorer. I found that the file is stored at this location, when I start the app in Debug mode for Windows target.

c:\Users\<username>\AppData\Local\Packages\5A71F6B4-07D1-4042-BC1C-C1FD1963A030_9zz4h110yvjzm\LocalState\app.db.

This is poor developer experience. How can I "mount" the database file from outside a .NET Maui Blazor app or specify the file path on the debugging host machine so that it's not random?

4
  • Open it with any SQLite db tool? Commented Feb 5, 2023 at 20:46
  • 2
    How can I specify a file location that is not generated randomly? I had to search my whole file system to find this file. Commented Feb 5, 2023 at 20:48
  • Is this the doc you used to know about builder.UseSqlite: Net Maui local databases? In your app, find what value is returned for FileSystem.AppDataDirectory. Is that the directory that was used? Commented Feb 5, 2023 at 22:58
  • @ToolmakerSteve I saw it. However, FileSystem.AppDataDirectory is not available for me. Commented Feb 6, 2023 at 8:03

2 Answers 2

5

The generated randomly file folder is {GUID}_{hash} which is generated randomly. If you want to specify the path that the db located and remove the randomly number, you can try to use below which is located in c:\Users\<username>\AppData\Local\:

string dataSource = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "app.db");

builder.UseSqlite($"Data Source={dataSource}");

Or you can use absolute path located in C:as Dimi suggested,

builder.UseSqlite(@"Data Source=C:\app.db");
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. Storing in c:\Users\<username>\AppData\Local\app.db does NOT work. That's what I already tried. However, storing in C:\app.db works.
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "app.db"); is NOT working. So I accepted Dimitri's answer.
What do you mean "does not work"? Do you get an error? Which path do you get from Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "app.db");
2

You can specify the database location by providing an absolute path in the connection string:

options.UseSqlite("Data Source=C:\your\path\app.db");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.