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.
Had the same problem. In my case i was missing some dependencies, so make sure that you have the following one :
Hope this would help. :)
that's all it will work
Make sure you run VS as Administrator and have installed the following packages:
Scaffold
command is part of dbcontext
command in EF. Below are the details for successful scaffold
:
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With