1、安裝ef tools
首先確保本地netcore sdk安裝完畢,如下是卸載tool、安裝指定版本tool、更新到指定版本的tool 的命令:
dotnet tool uninstall --global dotnet-ef
dotnet tool install --global dotnet-ef --version 5.0.0
dotnet tool update --global dotnet-ef --version 5.0.5
2、在目標項目安裝Microsoft.EntityFrameworkCore.Tools nuget包
3、定位到指定項目解決方案下(..csproj文件不是sln) 讓cmd工作目錄定位到當前目錄
4、遷移dbcontext
dotnet ef migrations add InitialIdentityServerPersistedGrantDbMigration -c PersistedGrantDbContext -o Data/Migrations/IdentityServer/PersistedGrantDb
5、更新到數據庫
dotnet ef database update --context PersistedGrantDbContext
6、刪除指定上下文對應的數據庫(慎用)
dotnet ef database drop --context PersistedGrantDbContext
7、爲遷移創建sql腳本
dotnet ef migrations script 0 20220510070558_InitialIdentityServerPersistedGrantDbMigration --context PersistedGrantDbContext -o Data/Migrations/IdentityServer/PersistedGrantDb/1.sql
注意:指令中的script 0 代表爲20220510070558_InitialIdentityServerPersistedGrantDbMigration創建遷移腳本,如果不加0,如下:
dotnet ef migrations script 20220510070558_InitialIdentityServerPersistedGrantDbMigration --context PersistedGrantDbContext -o Data/Migrations/IdentityServer/PersistedGrantDb/1.sql
則代表爲20220510070558_InitialIdentityServerPersistedGrantDbMigration之後的遷移創建遷移腳本.
dotnet tool update --global dotnet-ef