The term 'scaffold-dbcontext' is not recognized as the name of a cmdlet, function, script file, or operable program

When trying to scaffold with asp.net core this command

scaffold-dbcontext "Data Source=(local);Initial
Catalog=MyDb;Integrated Security=True;"
Microsoft.EntityFrameworkCore.sqlserver -outputdir Models

Gives this error.

scaffold-dbcontext : The term 'scaffold-dbcontext' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1

+ scaffold-dbcontext "Data Source=(local);Initial Catalog=MyDB;In ...
+ ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (scaffold-dbcontext:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

I have tried the solution here, but it does not work for me.

Any idea what the cause/cure could be?


Solution 1:

For me apparently it worked once I have also ran in Package Manager console :

 Install-Package Microsoft.EntityFrameworkCore.Tools 

Also make sure :

  • To have other dependencies (for example Microsoft.EntityFrameworkCore, Microsoft.EntityFrameworkCore.SqlServer, Microsoft.EntityFrameworkCore.SqlServer.Design...) referenced depending of your needs.

  • To select the right assembly as target for your commands in the top-right corner of the PM console (I am frequently fooled by forgetting it...)

Another problem I encountered : with the dbcontext located in a separate class library, I was encountering the following error :

Unable to find provider assembly with name Microsoft.EntityFrameworkCore.SqlServer. Ensure the specified name is correct and is referenced by the project.

Which I was able to fix by setting my class library as Startup project in VS (don't ask why as it seems meaningless, but it worked).

Late edit, there's something else to know : You can't run Scaffold-DbContext against a class library targetting only .Net Standard, you must also enable netcoreapp in it, or Scaffold-DbContext will complain. To support both targets, edit the csproj to put : <TargetFrameworks>netcoreapp2.2;netstandard2.0</TargetFrameworks> Instead of <TargetFramework> section.

After all these you'll be able to run your Scaffold-DbContext command line with proper arguments and connection string.

Solution 2:

Had the same problem. In my case i was missing some dependencies, so make sure that you have the following one :

  • Microsoft.EntityFrameworkCore
  • Microsoft.EntityFrameworkCore.SqlServer
  • Microsoft.EntityFrameworkCore.Tools

enter image description here

Hope this would help. :)