简体   繁体   中英

'dotnet ef' command is not working in .NET Core 2.2

I was trying to use Visual Studio Code as the IDE. When I needed to use the 'add-migrations' command, I came across the situation of using the 'dotnet ef migrations add name' command, but when I try to use this command I get the error:

dotnet : Could not execute because the specified command or file was not found.
At line:1 char:1
+ dotnet ef
+ ~~~~~~~~~
    + 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.

I've tried several solutions and none worked, is there any way out?

I'm using .NET Core 2.2.

UPDATE!

Now that you have the tool working, we can tell the it where to locate the context.

You mention you have separate projects for the API and Data, so let's name them like that:

Let's assume you have this structure:

Root:
    ├───Api:
    |     Api.csproj
    ├───Data:
    |     Data.csproj
    |     YourDbContext.cs

The Api and Data projects are separated and you have an EF DbContext called YourDbContext.

This is what you need to do. Navigate to the Api directory.

> cd Api
> dotnet ef migrations add Initial --project ..\Data\Data.csproj --context YourDbContext

This is what's happening:

We're running the tool from the main Api project and using --project to flag the Data project. I've also added the --context parameter just in case you have more that one, you know how to point to one of them.

Please, go ahead and try and let me know how this works.

Initial Approach

OK, I ran the command from a project directory using the version 3.0.100 of the dotnet sdk :

See the output:

> 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.

This is exactly the same output you're getting, so my guess is that you think you're using version 2.2 but maybe you're not.

You need to run that command from above from your solution/project directory, and verify the SDK is not a 3.x.

You can also "force" the sdk to be 2.2.402 for example if you create a global.json file in the project root with the the below content:

{
  "sdk": {
    "version": "2.2.402"
  }
}

This way, regardless of the SDK you have installed in your system, dotnet will use the version 2.2.402 (or the one you set in the file).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM