Waldemar
Waldemar

Reputation: 5513

Apply EF migrations in GitHub workflow

I have a GitHub repository for my Asp Net Core project with EF Core 3.0. I added the following workflow to run each time on updating the develop branch

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

The last line returns with an error:

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.

How can I apply the latest migrations to the target database using this workflow?

Upvotes: 5

Views: 8385

Answers (3)

Neal Ganslaw
Neal Ganslaw

Reputation: 101

None of the above worked and going to Windows Server is not a solution for me. Note: I am using a self-hosted Ubuntu 20.04.4 LTS runner with practically nothing installed. I got it working like this.

First, make sure Dotnet is installed.

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

Next, create a step to install the dotnet-ef tool using a new manifest. This solves the issue of not being able to install the tools if they're already installed globally.

    - name: Install dotnet-ef tools
      run: |
        dotnet new tool-manifest
        dotnet tool install dotnet-ef

Finally, run your migrations using the dotnet tool run dotnet-ef command

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

I do hope this helps someone.

Upvotes: 2

Waldemar
Waldemar

Reputation: 5513

I also had to add commands to install EF tool and to restore all tools to make my workflow work correctly:

- 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

Upvotes: 9

smac89
smac89

Reputation: 43068

You might want to run your workflow in a windows environment, i.e. using windows-latest rather than ubuntu-latest. You can see the software installed on windows here.

For Windows Server 2019, it says:

PATH: contains location of dotnet.exe

There is no mention of dotnet being on the PATH for the Linux environments

Upvotes: 0

Related Questions