Troubleshooting the dotnet ef command for EF Core Migrations
When using EF Core in a .NET Core app (ASP.NET Core or other app sitting on .NET core), it’s easy to run into a problem when attempting to use EF Core migrations at the command line. The most common one is
No executable found matching command "dotnet-ef"
In my EF Core course on Pluralsight, I chose to stick with VS 2015 for the .NET Core demos. VS2017 has the latest tooling and supports msbuild. But it was too bleeding edge (not even released) for my purposes — though I did demo with RC4 at the end of the course. VS2015 only supports project.json and the project templates set you up for .NET Core 1.0, not .NET Core 1.1. So in the course means that we’re stuck with project.json support and tooling that’s not quite aligned. But Pluralsight and I both agreed that it made sense not to ALSO force users to the bleeding edge, not even released VS2017 for the demos. The course’s focus is on EF Core, so as long as I could hand-hold users through the project.json setup stuff without the need to make them expert at that, it was the right way to go.
Of the nearly 2000 who have already watched the course since it’s release less than 2 weeks ago, a few people ran into some confusion with the versioning and getting the “no executable found” message. I worked through these with them but wanted to write down the suggestions I’d made and have a single blog post I could point to.
There are a few key things to watch out for.
The current stable tooling for EF Core migrations is split into two packages.
Microsoft.EntityFrameworkCore.Tools is for PowerShell
Microsoft.EntityFrameworkCore.Tools.DotNet is for the CLI (dotnet commands)
Make sure you’ve referenced the Tools.DotNet version of the package so that you have access to the CLI commands. If you’re following my course, that’s explained.
Make sure that you are running the command from the folder that contains the project where the Tools package is referenced. This is explained in the course demos, but still a step you may overlook in your excitement!
Make sure that you have the Tools package in the Tools section, not the dependencies section.
"tools": {
"Microsoft.EntityFrameworkCore.Tools.DotNet":
"1.1.0-preview4-final"
}
Make sure that the version of the EF Core tools you are using aligns with the version of .NET Core on your machine.
In my EF Core course, I’m using EF Core 1.1 and for EF tools, Microsoft.EntityFrameworkCore.Tools.DotNet 1.0.0-preview4.
You’ll need .NET Core 1.1 installed and the related SDK – which is not numbered as simply. Today the .NET Core SDK tools are still in preview so the version number of the current “stable” build that goes with .NET Core 1.1 is 1.0.0-preview2-1-001377. Note that on March 7 when VS2017 has its official release, the .NET Core SDK tools will also be released so the versions will just be normal numbers like 1.0.0.
With the “dotnet ef” command will tell you if you don’t have .NET Core 1.1 installed:
The specified framework 'Microsoft.NETCore.App', version '1.1.0' was not found.
- Check application dependencies and target a framework version installed at:
C:\Program Files\dotnet\shared\Microsoft.NETCore.App
- The following versions are installed:
1.0.1
- Alternatively, install the framework version '1.1.0'.
You can get the correct version via the download grid at microsoft.com/net/download/core. The grid only exposes stable releases. If you’re looking for nightly builds, there’s a link to those just below the grid.
The set of downloads you get via the LTS button is for .NET Core 1.0.3. The Current button gives you the latest stable versions. The SDK button gets you the Runtime + SDK. Whereas the Runtime button gives you ONLY the runtime.
I’m on windows 64bit, so selecting the Current, SDK for x64 is the path I want.
After it’s installed, typing dotnet will show you that you have the 1.1.0 version of .NET Core. dotnet –version gives you the version of the SDK. That should now be 1.0.0-preview2-1-001377,
If you installed this SDK but are still seeing the old SDK version (1.0.0-preview2-001313), that is likely because that version is specified in the global.json file of your solution. That shouldn’t create a problem for using the migration commands, but it’s a good idea to have the correct version listed in global.json.
Some additional tips for you!
Commands can only run from an executable/test project
In my Pluralsight EFCore course, I have the DbContext in its own class library project. So when running dotnet ef, after solving all of the above problems, you’ll get a new message which is just pointing out that the commands depend on an executable to run. That message looks like this:
Could not invoke this command on the startup project 'TestEFCore.Data'. This version of the Entity Framework Core .NET Command Line Tools does not support commands on class library projects in ASP.NET Core and .NET Core applications.
In my case, I wasn’t ready to add a UI or test to the solution, so I added a minimal console app just to cover this need. And it needs to reference the project with the DbContext. Once that’s sorted, you can use the —startup-project parameter of the dotnet ef command to point to that project. While I show all of this in my course, you can also see that in my MSDN Magazine article here: msdn.microsoft.com/magazine/mt742867
This will get a touch easier with the msbuild version of the EF Core tools. With the newer tooling, you’ll at least be able to run dotnet ef to get the command’s help without pointing to a startup-project, although to run sub commands, you’ll still need to specify the startup project.
Installing the EF Core Tools via Nuget in Visual Studio 2015
One of the viewers of the course reported a strange problem. He was able to add the EF Core Tools package in project.json and use the migrations from the CLI as expected. But if he attempted to add the package from the NuGet Package Manager or the Package Manager Console instead, he was back to the old ‘No executable found matching command “dotnet-ef”‘ error.
He finally noticed that there were errors when NuGet attempted to download the package. But in his IDE the errors were subtle, so he was unaware that the tools package was not installed. Here’s a screenshot he shared:
I figured out how to get myself into a similar bind and got a much more helpful error message:
Package 'Microsoft.EntityFrameworkCore.Tools.DotNet 1.1.0-preview4-final' uses features that are not supported by the current version of NuGet. To upgrade NuGet, see http://docs.nuget.org/consume/install....
Even though Visual Studio extension manager did not indicate an available update AND it listed my NuGet Package Manager version as 3.5 (the latest), I learned that the team responsible for this extension had temporarily stopped pushing notifications for updates! That change is noted in the “No Auto Updates” section of this blog post: http://blog.nuget.org/20161027/Announ...
So I had to explicitly download the latest VSIX (ignoring the fact that it seemed to have the same version # as the one I had installed!) from the NuGet distributions page. After installing that, it resolved the problem of installing the EF Core tools package from the Package Manager and Package Manager Console.
There’s a sneak peek at EF Core with msbuild in VS2017
A few people mentioned to me that they decided to go straight to VS2017 when working through the demos in the course. And they also said they were confused by some differences and had to do some research. I know for sure that one of these devs was kicking himself when I asked if he had watched the VS2017 demo I did at the end of the course before trying to VS2017 along with the early demos. (His answer was “umm, now you tell me!”) It’s listed in the table of contents for the course. So if you take a peek at that first, I think doing all the demos in VS2017 will be a lot easier!
Hope this helps!
Julia Lerman's Blog
- Julia Lerman's profile
- 18 followers
