Site icon Ryadel

How to fix the "No executable found matching command dotnet-ef" error in Visual Studio with .NET Core

How to fix the "No executable found matching command dotnet-ef" error in Visual Studio with .NET Core

If you've stumbled upon this post it probably means that you're trying to use Entity Framework Core in a .NET Core Visual Studio project with the dotnet ef CLI or Powershell command, which gives you the following error:

No executable found matching command "dotnet-ef"

This can be a nasty issue due to the fact that the framework gives little or no info, thus it's not easy to understand what it actually lies under the hood. Luckily enough, there are a few things you can try that will most likely help you to fix that for good.

The problem

The first thing we need to do is to understand these basic concepts:

  • The PATH environment variable has most likely nothing to do with the issue you're experiencing: as long as you can run the dotnet command with no issues you can just forget about it (and keep reading).
  • To use dotnet ef command from PowerShell you need the Microsoft.EntityFrameworkCore.Tools package library: however, if you want the CLI version, you also need the Microsoft.EntityFrameworkCore.Tools.DotNet package.
  • You can only use the dotnet ef command from a root project folder with contains a *valid* explicit reference to these packages within the project.json file (if you're dealing with a .NET Core 1.0 project) or within the <ProjectName>.csproj file (for .NET Core 1.1+ projects): if you try to execute it in a folder not containing a project file including these *valid* references, it won't work and it will return the above error.
  • These references were originally meant to be manually put in the JSON-based project.json file, firstly introduced when .NET Core 1.0 came out: adding them was easy back in the day, as we could easily access & edit that file from within the Visual Studio GUI. With .NET Core 1.1 they switched back to the XML-based <ProjectName>.csproj file, except they forgot to update the aforementioned package libraries so they could automatically add the required reference to it.

This pretty much summarizes it all.

The fix

Long story short, if you want to use dotnet ef command you have to do this (on .NET Core 1.1+ and .NET Core 2.x):

  • Add the Microsoft.EntityFrameworkCore.Tools and/or the Microsoft.EntityFrameworkCore.Tools.DotNet package libraries, depending if you want to use the PowerShell command or the CLI version. I personally do always install both of them.
  • Exit or switch away from the VS2017 GUI and edit your <ProjectName>.csproj manually: you need to create a new <ItemGroup>  element containing the required references (see below).
Be sure to change the version value to match the package release number: this is the latest stable as we're writing this, but it will definitely change in the near future.

If you already have other <DotNetCliToolReference>  elements, such as the Microsoft.VisualStudio.Web.CodeGeneration.Tools one, you can avoid creating a new <ItemGroup> and just add the new references in the already existing one in the following way:

In the unlikely case you end up with some NuGet package conflicts, you can try issuing the dotnet update command from the project's root folder to fix them: right after that, reload you project and try to execute the dotnet ef command again.

.NET Core 1.0 support

In the unlikely scenario you're hitting this issue with a .NET Core 1.0 project, you will have to add this to your project.json file instead:

That's about it: we sincerely hope that this post will help many .NET developers out there.

 

 

Exit mobile version