目錄
- 建立預設帶身份驗證 Blazor 程式
- 角色/元件/特性/過程邏輯
- DB 改 Sqlite
- 将自定義字段添加到使用者表
- 腳手架拉取IDS檔案,本地化資源
- freesql 生成實體類,freesql 管理ids資料表
- 初始化 Roles,freesql 外鍵 => 導航屬性
- 完善 freesql 和 bb 特性
本節源碼
https://gitee.com/densen2014/Blazor100/tree/Blazor-教程15-3/b15blazorIDS
引用 EntityFrameworkCore.Sqlite 庫
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.2" />
配置檔案加入Sqlite資料庫連結
appsettings.json檔案加入一行代碼"IdsSQliteConnection": "Data Source=ids.db;"
最終檔案如下
{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-b15blazorIDS-f969184b-89a5-4ccf-beeb-911a756ae70a;Trusted_Connection=True;MultipleActiveResultSets=true",
"IdsSQliteConnection": "Data Source=ids.db;"
},
...
}
使用EF Sqlite 配置
Program.cs檔案
//EF SqlServer 配置
// Add services to the container.
//var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
//builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(connectionString));
//EF Sqlite 配置
builder.Services.AddDbContext<ApplicationDbContext>(o => o.UseSqlite(builder.Configuration.GetConnectionString("IdsSQliteConnection")));
重新生成 Migrations 腳本
之前版本是基于localdb,如果不換腳本會出現An error occurred applying migrations, try applying them from the command line錯誤
删除 Migrations 檔案夾
可選: 保留sqlserver的Migrations腳本, 使用 從項目中排除 菜單
建立新遷移并為其生成 SQL 腳本
打開指令行, VS菜單欄=>工具=>Nuget包管理器=>程式包管理器控制台(Packge Manager Console), 執行以下指令
cd b15blazorIDS
dotnet ef migrations add idsSqlite
dotnet ef database update
完整流程
PM> cd b15blazorIDS
PM> dotnet ef migrations add idsSqlite
Build started...
Build succeeded.
Done. To undo this action, use 'ef migrations remove'
PM> dotnet ef database update
Build started...
Build succeeded.
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (7ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT COUNT(*) FROM "sqlite_master" WHERE "name" = '__EFMigrationsHistory' AND "type" = 'table';
...
Done.
PM>
重新生成的腳本
重新新增賬號
如果運作後出錯先跳過,直接導航到https://localhost:7011/Identity/Account/Register頁面注冊
Password | Confirm Password | |
[email protected] | 000000 | 000000 |
[email protected] | 000000 | 000000 |
自動生成的資料庫檔案
本節源碼
https://gitee.com/densen2014/Blazor100/tree/Blazor-教程15-3/b15blazorIDS
源代碼
https://gitee.com/densen2014/Blazor100 (鏡像/非最新版)
關聯項目
FreeSql
BA & Blazor
知識共享許可協定
本作品采用 知識共享署名-非商業性使用-相同方式共享 4.0 國際許可協定 進行許可。歡迎轉載、使用、重新釋出,但務必保留文章署名AlexChow ,不得用于商業目的,基于本文修改後的作品務必以相同的許可釋出。如有任何疑問,請與我聯系 。
轉載聲明
本文來自部落格園,作者:周創琳 AlexChow,轉載請注明原文連結:https://www.cnblogs.com/densen2014/p/17083934.html
AlexChow
今日頭條 | 部落格園 | 知乎 | Gitee | GitHub