I can run the following command locally to update my DB using Entity Framework Core migrations:
dotnet ef database update 0 --project src\MyProject\MyProject.csproj --startup-project src\MyStartupProject\MyStartupProject.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 src\MyProject\MyProject.csproj --startup-project src\MyStartupProject\MyStartupProject.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.
CodePudding user response:
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):
using var scope = app.Services.CreateScope();
using var appContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
appContext.Database.Migrate();
in classic program class:
var host = CreateHostBuilder(args).Build();
using var scope = host.Services.CreateScope();
var services = scope.ServiceProvider;
var context = services.GetRequiredService<ApplicationDbContext>();
context.Database.Migrate();
await host.RunAsync();
it will migrate if your database it's not up to date.
CodePudding user response:
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 of dotnet ef
which builds the project before attempting migrations (although I assume that simple running dotnet build
beforehand may have also worked).
dotnet-ef database update 0 --project src\MyProject\MyProject.csproj --startup-project src\MyStartupProject\MyStartupProject.csproj --context MyDbContext