.Net Maui builder.Services.AddDbContext(..) 未注册建筑服务 DI sqlite

问题描述 投票:0回答:1

错误是

SQLite Error 14: 'unable to open database file

我需要学习如何在我的 Maui 程序中成功使用 builder.Services.AddDbContext。我可以成功地使用经过验证的正确方法

new GlobalDbContext()
我更喜欢使用 DI 方法。我想我即将学习如何做到这一点,但仍然不知道我错过了什么。

还有其他人问过这个问题帖子 1年后仍然没有答案。 我正在使用 efcore 7.0.18 和 dotnet 7

异常表明我的数据库没有被发现。 当我走非注入路线时,我成功读取/创建数据库: 像这样: enter image description here

这是我注册服务的尝试 - 没有产生任何错误或警告(记下第 43 行):

enter image description here

第 41 行 GetConnectionString() 的代码经过充分测试,如下所示: enter image description here

当我在 ViewModel 中使用(构造函数注入)时,我编写如下代码:

enter image description here

在第 145 行我得到异常:

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

在 Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(Int32 rc,sqlite3 db) 在 Microsoft.Data.Sqlite.SqliteConnectionInternal..ctor(SqliteConnectionStringBuilder 连接选项,SqliteConnectionPool 池) 在 Microsoft.Data.Sqlite.SqliteConnectionPool.GetConnection() 在 Microsoft.Data.Sqlite.SqliteConnectionFactory.GetConnection(SqliteConnection 外连接)

连接字符串 droid

创建的连接字符串

数据源=/data/user/0/mil.navy.doma/files/GlobalDataBase.db

DI 连接字符串

数据源=/data/user/0/mil.navy.doma/files/GlobalDataBase.db

连接字符串 iOS

创建的连接字符串

数据源=/Users/mwardell/Library/Developer/CoreSimulator/Devices/E7D241C8-E6DF-444A-B321-624506E016E6/data/Containers/Data/Application/70874C2B-D545-413D-91BE-379C27DDA85E/Library/GlobalDataBase.db

DI 连接字符串 数据源=/Users/mwardell/Library/Developer/CoreSimulator/Devices/E7D241C8-E6DF-444A-B321-624506E016E6/data/Containers/Data/Application/70874C2B-D545-413D-91BE-379C27DDA85E/Library/GlobalDataBase.db

.net entity-framework-core maui
1个回答
0
投票

好的,在 DI / AddDbContext 的情况下,仍然需要 EnsureCreated 或 DoMigration。

© www.soinside.com 2019 - 2024. All rights reserved.