Resolving the "Command 'dotnet ef' Not Found" Issue When Creating Migrations

By FoxLearn 12/5/2024 10:53:32 AM   5
This guide will walk you through resolving the error when trying to create an Entity Framework Core (EF Core) migration and encountering issues like missing dotnet ef commands or version mismatches.

If you run the following command:

dotnet ef migrations add InitialCreate

If you get this 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.

To install the dotnet-ef tool, run the following command:

.NET 9

dotnet tool install --global dotnet-ef --version 9.*

.NET 8

dotnet tool install --global dotnet-ef --version 8.*

.NET 7

dotnet tool install --global dotnet-ef --version 7.*

.NET 6

dotnet tool install --global dotnet-ef --version 6.*

.NET 5

dotnet tool install --global dotnet-ef --version 5.*

.NET Core 3

dotnet tool install --global dotnet-ef --version 3.*

The dotnet ef tool is no longer included in the .NET Core SDK by default.

Instead, it is now distributed as a regular .NET CLI tool, which can be installed either globally (available across all projects) or locally (specific to a project). This change provides greater flexibility in managing the tool's version independently of the .NET Core SDK.

You can also resolve this issue by installing the dotnet-ef tool locally using the following steps:

Create a local tool manifest in the project directory:

dotnet new tool-manifest

Install the dotnet-ef tool locally with the specified version:

dotnet tool install --local dotnet-ef --version 5.0.6

Once installed, use dotnet dotnet-ef instead of dotnet-ef to execute commands.