installing dotnet ef tool in order to scaffold entity framework database context

In this blog post we’re going to be looking at creating the database context and models in Entity Framework Core specifically using command-line interface (CLI) tools. At the time of writing the target framework of my project was .NET Core 3.1 along with Entity Framework Core 3.1.4. I like to use the Package Manager Console as my CLI since, as a software developer, I’ll be using Visual Studio as my IDE so that’s an assumption I’ll be making for this post. So first thing I tried was to execute the following command.

dotnet ef dbcontext scaffold {Connection_String} --project {Project_Name} Microsoft.EntityFrameworkCore.SqlServer --use-database-names --output-dir {Output_Directory_Name} --context {Context_Name} --verbose --force

Problem was that as soon as I tried to execute that command I got the following error.

dotnet : Could not execute because the specified command or file was not found.
At line:1 char:1
+ dotnet ef dbcontext scaffold "Connection_String_In_Error, 1 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (Could not execu... was not found.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError

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.

First hiccup! Ok turns out that the dotnet ef tool is no longer part of the .NET Core SDK. I discovered this after a bit of digging and I even found the announcement by Microsoft themselves. So the next step was to install the dotnet ef tool and I’ve done that by executing this command

dotnet tool install --global dotnet-ef

That should install the latest version of the dotnet ef tool but for some reason it threw this error for me.

The tool package could not be restored.
At line:1 char:1
+ dotnet tool install --global dotnet-ef
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (The tool package could not be restored.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Tool 'dotnet-ef' failed to install. This failure may have been caused by:
* You are attempting to install a preview release and did not use the --version option to specify the version.
* A package by this name w
as found, but it was not a .NET Core tool.
* The required NuGet feed cannot be accessed, perhaps because of an Internet connection problem.
* You mistyped the name of the tool.

Second hiccup! Again, turns out that that command does not download and install the latest verison of the dotnet ef tool and in order to successfully install the tool you need to specify a version. Here’s the command including the latest version at the time of writing.

dotnet tool install --global dotnet-ef --version 3.1.8

Here’s a full list of versions of the dotnet ef tool. With our tool installed we can now go back to our original goal and scaffold our database in order to get the latest database changes. Well that’s a wrap and I hope this blog post has helped you as much as it has helped me.

Until next blog post,
Bjorn

Leave a comment