Friday, 17 January 2020

Running EF Commands in Builds with .NET Core 3.1 in Hosted Agents

When you run builds with Entity Framework (EF) commands such as dotnet ef migrations script with .NET Core 2.2 it would work without any issue. However, if you upgrade your projects to use .NET Core 3.1 your build may fail with issue below., when executing dotnet ef migrations script, to generate a script out of your EF migrations.
error NETSDK1045: The current .NET SDK does not support targeting .NET Core 3.1. Either target .NET Core 2.2 or lower, or use a version of the .NET SDK that supports .NET Core 3.1.
To resolve this as the first step you can set the build pipeline to use .NET Core 3.1 SDK, by using use .NET Core task.

Even after setting the SDK to 3.1 there will be a failure running dotnet ef migrations script command and any dotnet ef command.
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]Cmd.exe exited with code '1'.
This is due to that the EF tool comes as a separate NuGet package in which is not available by default in hosted agents (Hosted Windows 2019 with VS2019 was used in this build).

To resolve this issue, you can install the dotnet ef tool to hosted agent by adding a command line step which is executing below command.
dotnet tool install --global dotnet-ef --version 3.1.2

This will fix the issue in the build and it can now run dotnet ef migrations script and other dotnet ef commands.

No comments:

Popular Posts