-1

I was trying to use vscode as 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 r
un 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 dotnet core 2.2

Greg Gomes
  • 23
  • 6
  • https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/#create-a-migrations. I think it's simply add, not add-migration. – Viezevingertjes Oct 25 '19 at 11:55
  • Thanks for the help but it's not that i tried and receive the same error – Greg Gomes Oct 25 '19 at 12:16
  • 1
    Run `dotnet tool install -g dotnet-ef`. – Kirk Larkin Oct 25 '19 at 12:22
  • Have you installed the tool ? – Panagiotis Kanavos Oct 25 '19 at 12:23
  • @KirkLarkin I Guess that command only works for version 3.0 or above right ? – Greg Gomes Oct 25 '19 at 12:31
  • Nope. Works in 2.2 also. – Kirk Larkin Oct 25 '19 at 12:32
  • @PanagiotisKanavosI installed Microsoft.EntityFrameworkCore.Design – Greg Gomes Oct 25 '19 at 12:32
  • This should help: https://stackoverflow.com/questions/56862089/cannot-find-command-dotnet-ef/58155359 – Kirk Larkin Oct 25 '19 at 12:33
  • @KirkLarkin the part of dotnet ef runs but when a i get the part of `migrations add name` i get error that my appsetting.json is not the physical path of the context , but my context and API is separeted , there is any away to run this in that condition ? – Greg Gomes Oct 25 '19 at 12:44
  • 1
    @GregGomes this isn't error message you posted... – Tom Dee Oct 25 '19 at 12:47
  • @TomDee after run the command that said i get this another error , I just asking that it has another away to validate that command really works because when i tried to run this another time i get the error about incompatibilite version with the installtion of dot net ef by the coomand – Greg Gomes Oct 25 '19 at 12:53
  • @TomDee i guess a expressed my self in a wrong way in the comment above the commaand dotnet tool install -g dotnet -f works but whe try to run de migrations part i get another but when a tried this command sonner i get the error about incompability version – Greg Gomes Oct 25 '19 at 12:58

1 Answers1

1

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

Hope this helps!

Karel Tamayo
  • 3,455
  • 2
  • 21
  • 29
  • 1
    Thanks for the help but I already tried that away and still get the same Error. I'm sure that my version is 2.2 – Greg Gomes Oct 25 '19 at 12:20
  • @GregGomes, are you sure you have dotnet 2.2.x sdk installed instead of a dotnet runtime only? Can you show the ouput of your `dotnet --info` command. – Karel Tamayo Oct 25 '19 at 12:25
  • `netcoreapp2.2 ` This is my target framework in project a i just installed SDK 2.2.402 from [link]https://dotnet.microsoft.com/download/dotnet-core/2.2 – Greg Gomes Oct 25 '19 at 13:02
  • @GregGomes that's not the sdk. That the version of the framework you are targeting and that's a completely different thing. Did you run the command as I suggested? What's the ouput of `dotnet --version` ? – Karel Tamayo Oct 25 '19 at 13:03
  • I Know that but i don't installed the 3.0 And when a I created the procject the only target frameworks avaliable in machine was 2.2 – Greg Gomes Oct 25 '19 at 13:09
  • I think you really need to let us help you, so I'll ask again one more time, can you please post the output for `dotnet --version` when running that command from the root project directory? – Karel Tamayo Oct 25 '19 at 13:12
  • The result is that 2.2.402 – Greg Gomes Oct 25 '19 at 13:21
  • Excellent! Thank you. Now can you run `dotnet ef` and tell me the output of that one? – Karel Tamayo Oct 25 '19 at 13:22
  • After restart my machine I get the return with draw that i guess its Unicorn, i guess that its working , but when a i run the complete command `dotnet ef migrations add name` i get the error that can not find the appsettings file but my solution have API project and Data project that are separated there is away to get the command working in a solution in this structure ? And Thanks – Greg Gomes Oct 25 '19 at 13:29
  • Nice! So now you need to tell the tool where is your context. See the update in the answer. – Karel Tamayo Oct 25 '19 at 13:34
  • Glad to help. Happy "dotnet-ing"! – Karel Tamayo Oct 25 '19 at 14:00