111

I am using .NET Core 2.0 on Arch Linux / Visual Studio Code and am trying to get EF tools to work, but I keep getting the error:

cannot find command dotnet ef

I've just about looked everywhere and none of the suggestions worked.

The result of running 'dotnet ef':

[wasiim@wasiim-PC WebApiServerApp]$ dotnet ef --help
Cannot find command 'dotnet ef', please run the following command to install

dotnet tool install --global dotnet-ef
[wasiim@wasiim-PC WebApiServerApp]$ dotnet tool list -g
Package Id            Version      Commands
---------------------------------------------------
dotnet-dev-certs      2.2.0        dotnet-dev-certs
dotnet-ef             2.2.3        dotnet-ef
[wasiim@wasiim-PC WebApiServerApp]$

This is the 'dotnet --info' result, if it's of help:

[wasiim@wasiim-PC WebApiServerApp]$ dotnet --info
.NET Core SDK (reflecting any global.json):
 Version:   2.2.105
 Commit:    7cecb35b92

Runtime Environment:
 OS Name:     arch
 OS Version:
 OS Platform: Linux
 RID:         arch-x64
 Base Path:   /opt/dotnet/sdk/2.2.105/

Host (useful for support):
  Version: 2.2.3
  Commit:  6b8ad509b6

.NET Core SDKs installed:
  2.2.105 [/opt/dotnet/sdk]

.NET Core runtimes installed:
  Microsoft.NETCore.App 2.2.3 [/opt/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download

This is my .csproj file:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Folder Include="wwwroot\" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Lucene.Net.Analysis.Common" Version="4.8.0-beta00005" />
    <PackageReference Include="Lucene.Net.QueryParser" Version="4.8.0-beta00005" />
    <PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.5" />
   <PackageReference Include="Lucene.Net" Version="4.8.0-beta00005" />
    <PackageGroup Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.0" />
    <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.4" />
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.3" />

  </ItemGroup>

</Project>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Wasiim Ouro-sama
  • 1,485
  • 2
  • 12
  • 15

14 Answers14

174

In my case, the tools folder didn't exist inside %USERPROFILE%\.dotnet\ so I had to run the command dotnet tool install --global dotnet-ef to install dotnet ef. Then I was able to run dotnet ef...

This was the result of the above install command:

This was the result of the above install command

Garth
  • 3,237
  • 2
  • 18
  • 28
164

Note to readers: If you haven't installed dotnet ef, you need to install it first: dotnet tool install --global dotnet-ef. The question-asker already did that. You need to do that first before the rest of this answer can help.

How to fix this

For Linux and macOS, add a line to your shell's configuration:

  • bash/zsh:

    export PATH="$PATH:$HOME/.dotnet/tools/"
    
  • csh/tcsh:

    set path = ($path $HOME/.dotnet/tools/)
    

When you start a new shell/terminal (or the next time you log in) dotnet ef should work.

For Windows:

See this question on how to add to the PATH environment variable.

You need to add %USERPROFILE%\.dotnet\tools to the PATH.

What's going on?

The .NET Core 3.0 (preview) version of this failure is much more illuminating:

$ dotnet ef
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.

The second and the third one both refer to dotnet trying to find a dotnet-ef command but can't find it. As the third point says, dotnet-ef is not in your path.

Here's what the docs say:

Global Tools can be installed in the default directory or in a specific location. The default directories are:

OS Path

Linux/macOS $HOME/.dotnet/tools

Windows %USERPROFILE%\.dotnet\tools

So, you should add $HOME/.dotnet/tools/ to your $PATH.

But also note this part from docs:

These locations are added to the user's path when the SDK is first run, so Global Tools installed there can be called directly.

So, it sounds like something went wrong. If you installed using a manual tarball, the SDK screwed up and you should report this bug to Microsoft. If you use a distribution package, they screwed up and you should report this as a bug to them.

omajid
  • 14,165
  • 4
  • 47
  • 64
  • 1
    None of these recommended fixes fixed my situation. In my situation the reported .NET Core program that does not exist looks like nonsense: dotnet-@C:\Users\myUser\AppData\Local\Temp\tmp75c598ec34944633925ed32feffbe40f.rsp – Jack Fox Oct 20 '19 at 19:19
  • Added a quick TLDR run through to get along. – Nicholas Oct 25 '19 at 06:26
  • 6
    This is NOT the correct answer. The correct answer is running 'dotnet tool install -g dotnet-ef' in Command Prompt. Only afterwards should you refer to the answer above to correctly set the PATH variable. – Johan Wintgens Dec 23 '19 at 12:49
  • 3
    @JohanWintgens I see what you are saying, but I think it's the answer to the question as asked. The question already includes a step running `dotnet tool install...`. If the question didn't include that, then you would be completely correct. – omajid Dec 23 '19 at 16:05
  • Well damn, it is indeed in the question. In my defense, I had to actually Ctrl + F to find it in that wall of text. Nice job on the answer! – Johan Wintgens Dec 30 '19 at 10:50
  • I already had this all in my PATH ok. Garth's answer worked for me – Stuart Dobson Aug 31 '20 at 06:38
  • so running ```export PATH="$PATH:$HOME/.dotnet/tools/"``` works for me until i restart a terminal session, is there a way to make this permanent other than adding it to my .zshrc? – Bird Dad Jun 25 '21 at 03:54
17

For those who encountered the issue after updating their Visual Studio or .NET Core package this is due to updates made in .NET Core 3 by removing dotnet ef from .NET Core and making it a separate package which can be installed via:

dotnet tool install -g dotnet-ef

For reference see this answer and this breaking change

Elnoor
  • 3,401
  • 4
  • 24
  • 39
10

I have tried all the previous answers, and it didn’t work on my Mac with the latest macOS v10.15 (Catalina) update.

If you are using .NET Core 3 version for instance, you need to run these commands:

export PATH="$PATH:$HOME/.dotnet/tools/"
export DOTNET_SKIP_FIRST_TIME_EXPERIENCE=false
export DOTNET_ADD_GLOBAL_TOOLS_TO_PATH=true

dotnet tool install --global dotnet-ef --version 3.0.0
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Harry Sarshogh
  • 2,137
  • 3
  • 25
  • 48
7
dotnet ef database update

If the above command gives you an error, please follow the below steps.

  1. check path %USERPROFILE%.dotnet\ exists or not

  2. if not then run the below command

    dotnet tool install -g dotnet-ef
    
  3. Now again check the path and set the environment variable for the below path

    %USERPROFILE%\.dotnet\tool
    
  4. Now in cmd, set the path where your database context file is present and then run the below command

    dotnet ef database update
    

Note: It worked me for .NET Core 3.0.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Darshan Dave
  • 645
  • 2
  • 9
  • 32
5

Regarding the path fix:

Be aware this adds the path to the User PATH, not the System PATH environment variable. When firing off a "Developer Command Prompt" or "Developer PowerShell" from Visual Studio, it does not use the User path variable. You'll need to add it to the System environment variable as well.

Also, you'll need to restart Visual Studio Code before the change takes effect.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Randy Slavey
  • 544
  • 4
  • 19
4

Sometimes when you install a new version of dotnet sdk, it hits the PATH.

You have to manually add the folder .dotnet/tools from your home directory to PATH.

Chandresh Khambhayata
  • 1,748
  • 2
  • 31
  • 60
baruchiro
  • 5,088
  • 5
  • 44
  • 66
2

For anyone struggling with this issue on JetBrains Rider, I tried all the solutions listed on this page and eventually went into Visual Studio and ran the dotnet tool install --global dotnet-ef --version 3.0.0 command there and then reopened in Rider and it worked.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sam Watson
  • 21
  • 2
  • 3
    This post isn't an actual attempt at answering the question. Please note [StackOverflow doesn't work like a discussion forum](http://stackoverflow.com/tour), it is a Q&A site where every post is either a question or an answer to a question. Posts can also have [comments](http://stackoverflow.com/help/privileges/comment) - small sentences like this one - that can be used to critique or request clarification from an author. This should be either a comment or a [new question](http://stackoverflow.com/questions/ask) – ρяσѕρєя K Jan 06 '20 at 23:00
  • 2
    Actually the answer helps by telling other people with this question exactly which command to use and how I resolved my issue- assuming the other answers on this page don't work- which for me it didn't. So I don't think you read it correctly. – Sam Watson Jan 23 '20 at 02:48
  • 1
    [Visual Studio](https://en.wikipedia.org/wiki/Microsoft_Visual_Studio) or [Visual Studio Code](https://en.wikipedia.org/wiki/Visual_Studio_Code)? They are two different things. The question mentioned Visual Studio Code. – Peter Mortensen Jun 10 '21 at 15:09
2

TL&DR: dotnet tool install -g dotnet-ef

Rationale: See the announcement for ASP.NET Core 3 Preview 4, which explains that this tool is no longer built-in and requires an explicit install.

Reference: https://devblogs.microsoft.com/dotnet/announcing-entity-framework-core-3-0-preview-4/

FlyingV
  • 2,207
  • 19
  • 18
1

I had the same issue on Ubuntu 20.04.1 with the .net Core 5.0.0-rc.1 and the solution that worked for me was to install the dotnet ef as a local tool

https://learn.microsoft.com/en-us/dotnet/core/tools/global-tools#install-a-local-tool

dotnet new tool-manifest
dotnet tool install  dotnet-ef --version 5.0.0-rc.1.20451.13
Vaibhav
  • 1,156
  • 2
  • 12
  • 27
1
dotnet nuget add source --name nuget.org https://api.nuget.org/v3/index.json

dotnet tool install --global dotnet-ef
Mohammad Fazeli
  • 648
  • 10
  • 13
0

This is due to updates made in .NET Core 3 by removing dotnet ef from .NET Core and making it a separate package.

  1. You need to add %USERPROFILE%.dotnet\tools to the PATH.

  2. If it does not exist, then run this command:

    dotnet tool install -g dotnet-ef
    
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

For me, closing the solution and restarting Windows solved the issue. But before that we need to follow the below steps to check if installation is done properly or not.

  1. Run the below command. If you don't see dotnet-ef then you proceed to step 2, else, go to step 3.

    dotnet tool list --global
    
  2. Go to path, %USERPROFILE%\.dotnet\tools and check if dotnet-ef is there or not. If not then you might need to run

    dotnet tool install --global dotnet-ef --version 3.1.4
    

    I believe you have already done this.

  3. Add this path %USERPROFILE%\.dotnet\tools to your environment variables, to see if you get it in your project or not.

  4. At last, you need to close your project and restart the window. In my case, this step solved the problem.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rishu Ranjan
  • 494
  • 4
  • 7
0

With EF Core 6.0 we can generate EF Bundles - MSDN

This solves above problem, the bundle can be run as regular executable file and will apply migrations. Just make sure you have your appsettings.json or secrets.json if using UserSecrets package along with generated binary.

user@shell~$ ./efbundle

PS> efbundle.exe

Project --project and --startup-project can also be specified just as with regular migrations

PS> dotnet ef migrations bundle --self-contained -r linux-x64 --project .\MyApp.Shared  --startup-project .\MyApp.ApiClient

I believe we should be able to also specify --connection your_connection_string here but I didn't try.