.net core 3.1 ef Migrations 使用 CLI 数据迁移及同步

本文为 CLI 方式执行数据结构同步及迁移,请确定当前电脑是否安装 .net core 运行环境

4.1. 打开 cmd ,执行:dotnet tool install --global dotnet-ef 如版本过低请输入:dotnet tool update -g 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,同步到数据库

posted @ 2019-12-24 15:45  皓月青峰  阅读(1204)  评论(0编辑  收藏  举报