如何找到.NET Maui sqlite数据库文件

huangapple go评论68阅读模式
英文:

How to find .NET Maui sqlite DB file

问题

我有一个.NET Maui Blazor应用程序,并通过Entity Framework Core配置了SQLite存储:

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

一切都正常工作。出于调试目的,我想从数据库资源管理器中访问SQLite数据库文件。我发现当我以Windows目标的调试模式启动应用程序时,文件存储在以下位置:

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

这不是开发者友好的体验。如何从.NET Maui Blazor应用程序外部"挂载"数据库文件,或者在调试主机机器上指定文件路径,以使其不是随机的?

英文:

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

builder.UseSqlite($&quot;Data Source=app.db&quot;)

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\&lt;username&gt;\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?

答案1

得分: 4

生成的随机文件夹名称是 {GUID}_{hash},是随机生成的。
如果您想指定数据库的路径并去掉随机数部分,您可以尝试以下方法,该方法位于 c:\Users\&lt;username&gt;\AppData\Local\ 目录下:

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

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

或者您可以像Dimi建议的那样使用绝对路径,位于 C: 中,

builder.UseSqlite(@"Data Source=C:\app.db");
英文:

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\&lt;username&gt;\AppData\Local\:

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

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

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

builder.UseSqlite(@&quot;Data Source=C:\app.db&quot;);

答案2

得分: 2

你可以通过在连接字符串中提供绝对路径来指定数据库位置:

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

英文:

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

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

huangapple
  • 本文由 发表于 2023年2月6日 04:44:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75355380.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定