将 Entity Framework Core SQLite 文件存储在项目相关子目录中

TC *_*Fox 6 c# sqlite entity-framework-core asp.net-core asp.net-core-2.0

我有一个使用 Entity Framework Core 和 SQLite Provider 的 ASP.NET Core 2.0 应用程序。我试图将 SQLite 数据库文件存储在子目录中(特别是data\database\sqlite.db),但是如果我将连接字符串设置为,Data Source=data\database\sqlite.db我一运行就会收到以下异常dbContext.Database.Migrate()

Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 14: 'unable to open database file'.

如果我将其设置为Data Source=sqlite.db它,它将按预期工作,不会引发异常(但不在我需要存储它的目录中)。我需要做什么才能将 SQLite 数据库文件存储在相对于当前工作目录的子目录中?

为了上下文:我需要将 SQLite 数据库文件存储在子目录中,因为该应用程序在 Docker 容器中运行,该特定目录映射到 Docker 卷,因此在替换容器时数据库仍然存在。

小智 0

你应该修改为"Data Source=./data/database/sqlite.db"

"DefaultConnection": "DataSource=./data/database/sqlite.db"