本文為 CLI 方式執行數據結構同步及遷移,請確定當前電腦是否安裝 .net core 運行環境
4.1. 打開 cmd ,執行:dotnet tool install --global dotnet-ef 如版本過低請輸入:dotnet tool install --global dotnet-ef
4.2. 安裝最新的 .NET Core SDK 3.0
https://dotnet.microsoft.com/download/dotnet-core/3.1
4.3. cd D:\work\項目文件夾 (進入到項目的目錄),安裝 EF Design dotnet add package Microsoft.EntityFrameworkCore.Design
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
4.4. 驗證工具是否已經安裝好
dotnet restore
dotnet ef
_/\__
---==/ \\
___ ___ |. \|\
| __|| __| | ) \\\
| _| | _| \_/ | //|\\
|___||_| / \\\/\\
Entity Framework Core .NET Command-line Tools 3.1.0
4.5. 第一次執行(新增遷移任務)
dotnet ef migrations add initial
Build started... Build succeeded. Done. To undo this action, use 'ef migrations remove'
4.6. 有模型變化時,執行變更任務 dotnet ef migrations add testTable
然后運行項目就可以自動同步數據結構. 數據初始化見 MigrateHostedService.cs, 也可以執行 dotnet ef database update,同步到數據庫