在GitHub工作流中应用EF迁移

5

我有一个 Asp Net Core 项目,与 EF Core 3.0 相关,并在 GitHub 上建立了一个仓库。我添加了以下工作流程,以便在更新 develop 分支时每次运行。

name: Develop Build
on:
  push:
    branches:
      - develop
  pull_request:
    branches:
      - develop
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1
    - name: Setup .NET Core
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 3.0.102-servicing-014397
    - name: Build with dotnet
      run: dotnet build --configuration Release
    - name: Test with dotnet
      run: dotnet test --configuration Release
    - name: Update database
      run: dotnet ef database update --c DataContext --p MyProj --s MyProjFactory

最后一行返回了一个错误:
Could not execute because the specified command or file was not found.
Possible reasons for this include:
  * You misspelled a built-in dotnet command.
  * You intended to execute a .NET Core program, but dotnet-ef does not exist.
  * You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.
##[error]Process completed with exit code 1.

如何使用此工作流将最新的迁移应用于目标数据库?

1
在3.0版本中,dotnet ef命令不会默认安装:https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet - joakimriedel
我已经执行了这个命令 dotnet tool install --global dotnet-ef,但是在运行数据库更新时仍然出现错误。 - Waldemar
什么错误?你有安装全局的权限吗?你应该使用本地工具,参见文档。 - joakimriedel
@jcemoller,错误信息显示工具'dotnet-ef'(版本'3.1.0')已成功安装。 运行“dotnet tool restore”以使“dotnet-ef”命令可用。 因此,我添加了“dotnet tool restore”并解决了问题。 - Waldemar
3个回答

9

我还需要添加命令来安装EF工具,并恢复所有工具以使我的工作流正常运行:

- name: Update database
  run: |
    dotnet tool install --global dotnet-ef
    dotnet tool restore
    dotnet ef database update -c DataContext -p MyProj -s MyProjFactory
  env:
    ASPNETCORE_ENVIRONMENT: Development

3
针对我的情况,应用程序服务会从配置中获取数据库连接字符串,但现在它报错说缺少该连接字符串。你如何向 GitHub Actions 提供连接字符串? - Dušan Rychnovský
我的网站托管在AWS上,我正在使用此操作来访问机密并将其作为环境变量使用:https://github.com/abhilash1in/aws-secrets-manager-action。我相信你可以在Azure中找到类似的东西。 - Krusty

1

以上方法均无效,而且转向Windows Server对我来说也不是解决方案。注意:我正在使用一个自托管的Ubuntu 20.04.4 LTS运行程序,几乎没有安装任何东西。我是这样让它工作的。

首先,请确保已安装Dotnet。

    - name: Setup Dotnet
      uses: actions/setup-dotnet@v2
      with:
        dotnet-version: ${{ env.DOTNET_VERSION }}

接下来,创建一个步骤,使用新的清单安装dotnet-ef工具。这解决了如果已经全局安装了工具,则无法安装工具的问题。
    - name: Install dotnet-ef tools
      run: |
        dotnet new tool-manifest
        dotnet tool install dotnet-ef

最后,使用dotnet工具运行dotnet-ef命令来运行您的迁移

     - name: Create DB Migration Script
       run: |
        cd src/project
        dotnet tool run dotnet-ef migrations script -i -v -o "${{ github.workspace }}/dbscripts/Migrations.sql"

我真心希望这能帮到某个人。


0

你可能想在Windows 环境中运行你的工作流程,即使用windows-latest而不是ubuntu-latest。你可以在这里查看安装在Windows上的软件here

对于Windows Server 2019,它说:

PATH:包含dotnet.exe的位置

在Linux环境中没有提到dotnet在PATH上的位置。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接