從註冊DbContext開始,與ASP.NET CORE MVC 有明顯差異…
.NET MAUI 的 ToDoListContext 內容即與 ASP.NET CORE MVC 不同。
需要多新增一個PathDB來指定SQlite路徑。
需在要使用的頁面使用以下程式碼註冊:
@inject IDbContextFactory<ToDoListContext> DbFactory
後續方可使用dbContext,範例如下:
page "/"
@using MauiApp4_Reminder.Models
@using MauiApp4_Reminder.Services
@using Microsoft.EntityFrameworkCore
@inject ToDoListService toDoListService
@inject IDbContextFactory<ToDoListContext> DbFactory
<h1>Hello, world!</h1>
Welcome to your new app.
@code {
private ToDoListContext _dbContext;
protected override void OnInitialized()
{
_dbContext = DbFactory.CreateDbContext();
base.OnInitialized();
}
}
但是後續執行 dotnet ef migrations add InitialDB,遇到了以下錯誤:
The specified deps.json [D:\Users\XXXX\source\repos\MauiApp4_Reminder\MauiApp4_Reminder\bin\Debug\net8.0-android\MauiApp4_Reminder.deps.json] does not exist
上網搜尋未果,.NET 8 MAUI BLAZOR with SQLite只能暫時止步於此了。