I can run the following command locally to update my DB using Entity Framework Core migrations:
dotnet ef database update 0 --project srcMyProjectMyProject.csproj --startup-project srcMyStartupProjectMyStartupProject.csproj --context MyDbContext
However when I try to run it in a GitHub action I get an error.
The action:
name: Migrate DB
on:
workflow_dispatch:
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Check out repository code
uses: actions/checkout@v3
- name: Migrate DB
shell: pwsh
run: |
dotnet tool install --global dotnet-ef
dotnet tool restore
dotnet ef database update 0 --project srcMyProjectMyProject.csproj --startup-project srcMyStartupProjectMyStartupProject.csproj --context MyDbContext
The error is:
/home/runner/work/MyProject/MyProject/src/MyProject/MyProject.csproj :
error MSB4057: The target "GetEFProjectMetadata" does not exist in the
project. Unable to retrieve project metadata. Ensure it’s an SDK-style
project. If you’re using a custom BaseIntermediateOutputPath or
MSBuildProjectExtensionsPath values, Use the
–msbuildprojectextensionspath option.
As far as I know I’m not using a custom BaseIntermediateOutputPath or MSBuildProjectExtensionsPath values. The projects are a dotnet 6
web project and class library built in C#
.
I’ve tried adding --msbuildprojectextensionspath
with the value obj/local
as suggested in similar questions but it has had no effect.
2
Answers
Bit of a face-palm. It turns out it was because I was not first building the project in the pipeline which meant that the git ignored items were not there. I assume the issue was the missing
obj
folder.The solution was to use
dotnet-ef
instead ofdotnet ef
which builds the project before attempting migrations (although I assume that simple runningdotnet build
beforehand may have also worked).instead of adding this command in your github yml file you can migrate database in your program class:
in new program class (ASP .NET 6):
in classic program class:
it will migrate if your database it’s not up to date.