238

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?

2
  • 3
    Make sure that entityframework.tools is in the tools section of your project.json.
    – Tim Scriv
    Commented Sep 14, 2016 at 3:07
  • May be you are running in Developer tools, instead of package manager console.. ?!
    – HydTechie
    Commented Jul 31, 2023 at 9:48

12 Answers 12

439

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.

-- 2022 update --

I'm glad to see that post is still helpful as it receives some new upvotes, but instead of command-line scaffolding, there's a newer solution for the happy users of VS : you can rely on the retro-engineering feature of the extension EF Core Power tools.

I'm using it in all my new projects since a while and I find it much more powerful than raw command line, and it allows you to save your execution settings (which will avoid you to create a .bat with your custom command line). Of course, it's your choice.

6
  • 4
    Thanks for this. Your solution still works for VS2019 using .net core 2.2
    – PaulC
    Commented Apr 23, 2019 at 13:55
  • 3
    Too bad Microsoft did not improved this during these last months. I was not expecting my answer to be so helpful and for so long as some part of it appears to be a tricky workaround for some weird IDE behavior...
    – AFract
    Commented Apr 24, 2019 at 5:38
  • 5
    I ran into the error: Unable to find provider assembly with name Microsoft.EntityFrameworkCore.SqlServer. Ensure the specified name is correct and is referenced by the project. I also had to run: Install-Package Microsoft.EntityFrameworkCore.SqlServer Commented Jul 21, 2019 at 5:38
  • 2
    @AhmedFaizan actually it is one of the examples of packages mentioned in the post. Glad it was helpful
    – AFract
    Commented Jul 22, 2019 at 5:26
  • 3
    Required by NET Core 3.1 but not by mysql official documentation, this must be installed in order to scaffold a mysql database first with mysql EF core. Thanks of course. Commented Sep 20, 2020 at 0:30
51

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

1
  • 4
    I have all 3 of those, but I'm still getting that "Scafford-DbContext is not recognized" error. I tried adding Microsoft.EntityFrameworkCore.SqlServer.Design, but that didn't help.
    – Rod
    Commented Oct 17, 2020 at 16:30
31
  1. Make sure that this is available in your project.json file "Microsoft.EntityFrameworkCore.Tools.DotNet": "1.0.0-preview3-final".

enter image description here

  1. Run the command in the package manager console

enter image description here

that's all it will work

2
  • 12
    actually i fix this by installing nuget package : Install-Package Microsoft.EntityFrameworkCore.Tools
    – kepung
    Commented Aug 16, 2017 at 3:07
  • 1
    @kepung - I had to do this after a recent VS2017 update. Odd. Thank you for your comment as it resolved the problem for me.
    – KSwift87
    Commented Mar 20, 2018 at 15:49
27

Make sure you run VS as Administrator and have installed the following packages:

  • Microsoft.EntityFrameworkCore.SqlServer
  • Microsoft.EntityFrameworkCore.SqlServer.Design
  • Microsoft.EntityFrameworkCore.Tools
1
  • 3
    For my similar problem, I didn't need to install Microsoft.EntityFrameworkCore.SqlServer.Design and I installed the rest and it runs correctly.
    – MinaMRM
    Commented Oct 4, 2020 at 7:44
23

Scaffold command is part of dbcontext command in EF. Below are the details for successful scaffold:

enter image description here

Package references required:

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.0.0"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.0"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.0.0"/>

So our scaffold command should look like:

dotnet ef dbcontext scaffold "Server=localhost\SQLEXPRESS;Database=MyDatabase;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o OutputDirectory

Your Server value might differ as per your Db server name. Replace MyDatabase with your Database name like master and OutputDirectory to the location you want your newly created scaffolded classes like Models folder.

21

Make sure you are using the right console, the "Package Manager Console". There is also a "Terminal" console which looks very similar, but doesn't work for this command. Package Manager Console can be found in View -> Other Windows (as of Visual Studio 2019, ver. 16.6.5, Windows OS)

3
  • I open Package Manager Console from View -> Other Window -> Package Consol ... but this consol is uneditable ... I am using VS22 of m1 machine ... maybe it's a preview edition of VS that's why it's happening. Commented Jul 25, 2022 at 13:27
  • Package Console does not work on mac m1... user Terminal instead ... adding dotnet ef in front of every command works for me Commented Jul 25, 2022 at 14:50
  • This answer worked for me. I was following a tutorial and it said to use the "Tools -> Command line -> Developer Command Prompt" which gave me the error Commented May 7 at 18:04
10

I had installed Microsoft.EntityFrameworkCore.Tools from NuGet Package Manager and it was visible in the installed packages. But I kept getting this error.

Restarting Visual Studio (2019/Version 16.4.4) fixed it for me.

10

With VS2022, none of them worked.

But I installed EF

dotnet tool install --global dotnet-ef

and changed the code;

dotnet ef dbcontext scaffold "Server=servername;Database=dbname;Persist Security Info=True;User ID=XXX;Password=YYY;" Microsoft.EntityFrameworkCore.SqlServer -o Models

Also I had to install .Net 6.0 hosting bundle https://dotnet.microsoft.com/en-us/download/dotnet/6.0/runtime?cid=getdotnetcore

2
  • finally ... this work for me on VS22 mac m1 machine... Is their is any difference between Scaffold-DbContext and dbcontext scaffold Commented Jul 25, 2022 at 14:20
  • Excellent Thank You
    – MrPython
    Commented Jun 20 at 14:10
4

If you're using .NetCore 2.2 then the command below works like a charm for me either in Command Prompt (CMD) or on Git Bash.
Make sure that you are directly on the project folder before running the command.

For example C:\App\ProjectName:

 dotnet ef dbcontext scaffold "Server=.\;Database=Databasename;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o Model
3

For me this error was caused by extra spacing was added around the hyphens, which was for some reason added when copy pasting from the command from the docs. Removing the spacing fixed it.

causes error:

Scaffold - DbContext "Server=(localdb)\mssqllocaldb;Database=myDbName;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer - OutputDir Models

the fix:

Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=myDbName;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

The docs also say if you receive this error, try restarting Visual Studio.

https://learn.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db

0
2

I ran into another cause of this error recently: NuGet itself was out of date.

Updating NuGet resolved the issue.

If Devanathan's answer doesn't work for you, check to make sure NuGet itself is up to date.

1

If your solution has more than one project and the EF files are not in the startup project, make sure you choose the correct project here

Package Manager Console drop-downs

Not the answer you're looking for? Browse other questions tagged or ask your own question.