一橙数、PostgreSQL 操作
1、將psql安裝目錄的 bin
目錄加入當(dāng)前用戶的path
環(huán)境變量急但,并重啟電腦
例如我的 psql.exe 所在目錄為:
D:\PostgreSQL\pg10\bin
- 運行 cmder篮绰,連接數(shù)據(jù)庫
psql --username=postgres
- 運行命令,創(chuàng)建數(shù)據(jù)庫
CREATE DATABASE archdb_dev;
然后命令 \l
查看數(shù)據(jù)庫
20180728162044.png
或者直接使用圖形界面工具 pgAdmin 進(jìn)行操作:
20180728162542.png
- 回到 Solution Folder缓升,Data 下面的
Migrations
目錄中包含的數(shù)據(jù)庫遷移文件是針對 sqlite 的,我們不用蕴轨,所以直接刪除該文件夾港谊。刪除后Data下只有一個DbContext類
20180728163213.png
二、接下來添加自己的遷移文件:
- 命令行進(jìn)入工程目錄橙弱,運行
dotnet ef
查看 ef 的安裝情況
20180728164145.png - 運行以下命令封锉,生成
ApplicationDbContext
的 Migrations
dotnet ef migrations add initial_identity_migration -c ApplicationDbContext -o Data/Migrations/AspNetIdentity/ApplicationDb
成功后會生成我們剛才命令里面的 -o 目錄
并且這樣生成的 Migration 文件才是我們所需針對 PostgreSQL 的
- 運行以下命令,生成
ConfigurationDbContext
的 Migrations
dotnet ef migrations add initial_is4_server_config_migration -c ConfigurationDbContext -o Data/Migrations/IdentityServer/ConfigurationDb
- 然后運行以下命令生成
PersistedGrantDbContext
的 Migrations
dotnet ef migrations add initial_is4_persisted_grant_migration -c PersistedGrantDbContext -o Data/Migrations/IdentityServer/PersistedGrantDb
都完成后我們的 Migrations 目錄會是這樣:
20180728170421.png
三膘螟、Update Database
- 運行命令,生成
ApplicationDbContext
相關(guān)表
dotnet ef database update --context ApplicationDbContext
20180728171301.png
- 接下來運行命令碾局,生成 IdentityServer 需要的相關(guān)表
dotnet ef database update --context ConfigurationDbContext
20180728171453.png
- 運行命令荆残,生成
PersistedGrantDbContext
相關(guān)表【一張:PersistedGrants】
dotnet ef database update --context PersistedGrantDbContext
全部完成后數(shù)據(jù)庫中會有以下26張表
20180728171804.png
四、Seed Data -- ApplicationDbContext
類SeedData
包含一個方法EnsureSeedData
净当,用來為新數(shù)據(jù)庫插入數(shù)據(jù)
查找引用發(fā)現(xiàn)其在 Program
類的Main
方法中被調(diào)用
public static void Main(string[] args)
{
var seed = args.Any(x => x == "/seed");
if (seed) args = args.Except(new[] { "/seed" }).ToArray();
var host = BuildWebHost(args);
if (seed)
{
SeedData.EnsureSeedData(host.Services);
return;
}
host.Run();
}
當(dāng)程序運行傳入?yún)?shù) /seed
時内斯,運行初始數(shù)據(jù)庫操作
+運行命令dotnet run /seed
,啟動程序執(zhí)行seed操作
20180728173300.png
完成后表【AspNetUsers】中生成
SeedData
中的兩條數(shù)據(jù)20180728173441.png
表【AspNetUserClaims】會有數(shù)據(jù)
20180728174036.png
五像啼、SeedData -- ConfigurationDbContext & PersistedGrantDbContext
接下來用另一種方式實現(xiàn)SeedData
- 在
Startup.cs
中編寫如下方法
private void InitializeDatabase(IApplicationBuilder app)
{
// using a service scope
using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{
// Create PersistedGrant database if not exist, then do migration
serviceScope.ServiceProvider.GetRequiredService<PersistedGrantDbContext>().Database.Migrate();
var configDbContext = serviceScope.ServiceProvider.GetRequiredService<ConfigurationDbContext>();
configDbContext.Database.Migrate();
// Seed data if not exist any
if (!configDbContext.Clients.Any())
{
foreach (var client in Config.GetClients())
{
configDbContext.Clients.Add(client.ToEntity());
}
configDbContext.SaveChanges();
}
if (!configDbContext.IdentityResources.Any())
{
foreach (var res in Config.GetIdentityResources())
{
configDbContext.IdentityResources.Add(res.ToEntity());
}
configDbContext.SaveChanges();
}
if (!configDbContext.ApiResources.Any())
{
foreach (var api in Config.GetApis())
{
configDbContext.ApiResources.Add(api.ToEntity());
}
configDbContext.SaveChanges();
}
}
}
然后在Configure
方法添加調(diào)用
- 運行程序
dotnet run
20180728180226.png
相關(guān)數(shù)據(jù)也已被初始化俘闯,并且IdentityServer也在正常運行
按照提示瀏覽器輸入網(wǎng)址http://localhost:5000
, 可以看到 IdentityServer 已正常運行
20180728180455.png