Attempting to Scaffold a database for use in a .net core MVC web app has turned in to a headache! Luckily, dear reader, I’ve suffered so you can avoid the pain.
The first thing to do is to make sure your .csproj file contains the following DotNetCliToolReference elements. In Visual Studio Community you can right click on your project –> Tools –> Edit File.
<ItemGroup> <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" /> <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" /> </ItemGroup>
Now even though Visual Studio will report that “Packages restored successfully” it turns out that the package “Microsoft.VisualStudio.Web.CodeGeneration.Tools” is actually a Command Line Interface (CLI) package that won’t be restored.
To make matters more confusing, if you try and add this Package through the Project –> Add Nuget packages… UI you’ll receive an error!
To restore the package successfully you must open up Terminal/Command Prompt/Bash and cd into your Project directory (not the Solution directory). Here you can run the command
Once this command completes running, you can now execute the following command to verify that the Entity Framework CLI tool has been installed.
dotnet ef -h
Now you can go ahead and run your scaffold command!
Authors note: I’m using the Npgsql EF Core Provider to connect to my PostgreSQL database. Additionally, there was a lot of googling before anyone mentioned manually running dotnet restore.