###### tags: `Web` `DotNet Core` `Entity Framework`
# EF Migration指令整理
參考網址:[https://www.cnblogs.com/wangjieguang/p/EFCore-Migration.html](http://example.com)
1) On PowerShell Interactive Window
2) 全部指令請參考 help 輸入: dotnet ef
3) 常用指令如下:
先跳到目錄下: cd src\HxCrmApi.Core or cd 'D:\00TFS\PJ2018017\API\hx-crm-api\src\HxCrmApi.Core'
打入指令: dotnet ef migrations add HxCrmApiDB_xxx ( 如果太多要移除,請輸入 dotnet ef migrations remove )
再打指令: dotnet ef database update ( 這個全部都會執行,如果要指定請在後面打 HxCrmApiDB_xxx )
4) 要執行開發環境的資料庫 請執行: $Env:ASPNETCORE_ENVIRONMENT = "Development"
5) 要執行正式環境的資料庫 請執行: $Env:ASPNETCORE_ENVIRONMENT = "Production"
6) 要輸出 SQL 請執行: dotnet ef migrations script > D:\HxCrmApiDB_xxx.sql ( 這個全部都會執行,如果要指定請在後面打 HxCrmApiDB_xxx )
7) 要輸出 SQL 請執行: dotnet ef migrations script HxCrmApiDB_xxx > D:\HxCrmApiDB_xxx.sql ( 這個全部都會執行,如果要指定請在後面打 HxCrmApiDB_xxx )
```powershell
Script-Migration [-From] <String> [-To] <String> [-Idempotent] [-Output <String>] [-Context <String>] [-Project <String>] [-StartupProject <String>] [<CommonParameters>]
Script-Migration -From 20171023035934_v113 -To 20171024035934_V114
```
上面的命令会生成113版本迁移到114版本的SQL语句
https://docs.microsoft.com/zh-tw/ef/core/managing-schemas/migrations/?tabs=dotnet-core-cli#revert-a-migration
> 安裝工具
安裝命令列工具:
針對 Visual Studio,我們建議使用套件管理員主控台工具。
針對其他開發環境,請選擇 .NET Core CLI 工具。
> 建立移轉
在您定義起始模型之後,即可開始建立資料庫。 若要新增初始移轉,請執行下列命令。
```powershell
Add-Migration InitialCreate
dotnet ef migrations add InitialCreate
```
> 自訂移轉程式碼
對您的 EF Core 模型進行變更後,資料庫結構描述會失去同步。若要將其更新為最新狀態,請新增另一個移轉。
您能夠以類似版本控制系統中認可訊息的方式來使用移轉名稱。
例如,如果變更是要檢閱的新實體類別,您可以選擇像是 AddProductReviews 的名稱。
```powershell
Add-Migration AddProductReviews
dotnet ef migrations add AddProductReviews
```
> 移除移轉
在您新增移轉時,有時候會發現您必須在套用 EF Core 模型之前對其進行其他變更。 若要移除上一個移轉,請使用此命令。
```powershell
Remove-Migration
dotnet ef migrations remove
```
> 還原移轉
若您已經套用移轉 (或多個移轉) 到資料庫但需要還原,您可以使用相同命令來套用移轉,但必須指定所要復原到的移轉名稱。
```powershell
Update-Database LastGoodMigration
dotnet ef database update LastGoodMigration
```
> 更新資料庫
接下來,將移轉套用到資料庫以建立結構描述。
```powershell
Update-Database
dotnet ef database update
```
> 產生 SQL 指令碼
對移轉進行偵錯或將其部署到生產資料庫時,產生 SQL 指令碼很實用。
您可以進一步檢閱程式碼的精確度,並對其進行微調以符合生產資料庫的需求。 指令碼也可以搭配部署技術使用。 基本命令如下。
```powershell
Script-Migration
dotnet ef migrations script
```
> 下列範例會使用遷移名稱來建立 InitialCreate 遷移的腳本。
```powershell
Script-Migration -To InitialCreate
```
> 下列範例會使用遷移識別碼,針對 InitialCreate 遷移之後的所有遷移建立腳本。
```powershell
Script-Migration -From 20180904195021_InitialCreate
```
> 異動資料庫&產生異動相關Sql Script -verbose 會顯示sql script
```powershell
update-database -verbose
```
> 產生不同migration版本間,所需要的異動SQL Script
> 產生最近一次差異的SQL(預設)
```powershell
Update-Database -Script
```
> 產生重初始化(0) 到第(2)版所有SQL 語法 (e.g Create All Table)
```powershell
Update-Database -Script -SourceMigration: $InitialDatabase -TargetMigration: secondMigration
```
> 產生差異Sql (1)到(2)版的差異SQL(e.g Alter Table)
```powershell
Update-Database -Script -SourceMigration: firstinit -TargetMigration: secondMigration
```
> 產生差異Sql (2)到(3)版的差異SQL(e.g drop column)
```powershell
Update-Database -Script -SourceMigration: firstinit -TargetMigration: thirdMigration
```
> 更多update-database相關指令
```powershell
get-help update-database
```
-------------------------------
> 常用語法
```powershell
Add-Migration
Update-Database
```
> 輸出異動的 SQL 不包含 From,產生 From 之後,並輸出到指定的資料夾
```powershell
Script-Migration -From XXX -o "D:\aaaaaa\ticket_0629-1.sql"
```