In this article, we are going to learn how to solve the Command or File Was Not Found EF Core migration error.
Let’s start.
Dotnet EF Not Found Error
Before we start, we want to mention that we are not going to talk about how to create migrations in EF Core. We assume you are already familiar with that. Of course, to learn more about EF Core Migrations, you can check out our article: Migrations and Seed Data with Entity Framework Core.
Now, let’s continue.
Customer.cs
class and we intend to create a database with a Customer table:public class Customer { public Guid Id { get; set; } public string? FirstName { get; set; } public string? LastName { get; set; } public int Age { get; set; } public string? Address { get; set; } public string? CardNumber { get; set; } }
Then, if we try to run EF Core migrations using dotnet ef migrations add InitialCreate
, we are going to get 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 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.
From this error, we understand that we have an issue related to our command and we also see several reasons that can cause it:
The first one – You misspelled a built-in dotnet command is basically not true because we use the command from Microsoft’s official documentation.
Let’s check the second one – You intended to execute a .NET Core program, but dotnet-ef does not exist. This can lead us to our solution for the issue since we are obviously missing something.
Let’s continue inspecting the last one, which mentions the global tool and a not found path. It should help us as well since we use a dotnet ef
command and it is not recognized on our machine, so it is somehow similar to the second reason.
So, for sure, we are missing something. But what is it, and how to fix it?
How to Fix the Command or File Was Not Found Error?
If we inspect this announcement for Entity Framework Core 3.0 Preview 4, we can find that the dotnet ef tool is no longer part of the .NET Core SDK. That’s why in our example, we can’t execute the migration.
That said, we should install the dotnet ef tool on our machine:
dotnet tool install --global dotnet-ef
Now we should be able to run our dotnet ef commands successfully.
Let’s execute our migration command again:
dotnet ef migrations add InitialCreate
And now we get a successful result:
Build started ... Build succeeded. Done. To undo this action, use 'ef migrations remove'
Then, to apply our changes to the database, we have to execute a new command:
dotnet ef database update
And if we inspect the message:
Build started ... Build succeeded. Applying migration '20220401210459_InitialCreate'. Done.
We can see that we’ve applied our migration successfully.