Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Scaffold-DbContext on Visual Studio for Mac

I have a site that was built using database first and I'm trying to continue development of it on a mac. Normally I would run the Scaffold-dbContext using the Console Package Manager in Visual Studio. The mac version doesn't have this I tried running it in Terminal, but that obviously didn't work. Is it possible to run this command, or do I need to continue development on Windows? 

like image 313
Jhorra Avatar asked Jun 12 '17 15:06

Jhorra


People also ask

What is Scaffold-DbContext command?

The above Scaffold-DbContext command creates entity classes for each table in the SchoolDB database and context class (by deriving DbContext ) with Fluent API configurations for all the entities in the Models folder. The following is the generated Student entity class for the Student table.

Is Scaffold-DbContext command is used in EF Code First approach?

Entity Framework Core supports Database-First approach via the Scaffold-DbContext command of Package Manager Console. This command scaffolds a DbContext and entity type classes for a specified database.

How to update ef core tools?

Update the toolsUse dotnet tool update --global dotnet-ef to update the global tools to the latest available version. If you have the tools installed locally in your project use dotnet tool update dotnet-ef . Install a specific version by appending --version <VERSION> to your command.


2 Answers

Here is the code work for me on visual studio mac

Install the below package using visual studio mac edit references on project or add package to .csproj file.

Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
Microsoft.VisualStudio.Web.CodeGeneration.Design

or using the Terminal navigate to the project and use the below command -

dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.EntityFrameworkCore.Tools
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design

Now check the tools and EF are installed or not.Navigate to the project install location and use mac terminal with below command. It should show entity framework details

dotnet ef

Now Scaffold the DB context

dotnet ef dbcontext Scaffold "Server=<servername>,1433;Initial Catalog=<dbName>;Persist Security Info=False;User ID=<userID>;Password=<password>;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"Microsoft.EntityFrameworkCore.SqlServer -o <directory name>

References

https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet

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

https://www.learnentityframeworkcore.com/walkthroughs/existing-database

like image 69
San Jaisy Avatar answered Oct 14 '22 05:10

San Jaisy


You can run the command from the terminal after completing a few required steps as found here:

  1. You need to add the following manually to your *.csproj

<ItemGroup>
  <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
</ItemGroup>
  1. Execute

dotnet add package Microsoft.EntityFrameworkCore.Design

3.Execute

dotnet restore

You should now be able to scaffold using the command:

dotnet ef dbcontext scaffold --help

like image 7
Paul v Zyl Avatar answered Oct 14 '22 04:10

Paul v Zyl