LAB SERIES #1 : Scaffold entities from database schema in EF Core 3.1

1/ Install packages

EF Core tools:

Install-Package Microsoft.EntityFrameworkCore.Tools

SQL Server data provider for EF Core:

Install-Package Microsoft.EntityFrameworkCore.SqlServer

 

2/ Scaffolding

Powershell command (run from Powershell or Package Manager console):

Scaffold-DbContext “Data Source=Your_Database_Name;Initial Catalog=AdventureWorks2017;Integrated Security=true;” -Provider Microsoft.EntityFrameworkCore.SqlServer -Context AdventureWorkContext -OutputDir ./Entities

Arguments:

  • Connection string to the database where resides model/ schema to reverse engineer
  • Provider : underlying data provider for entity framework, which helps connect to database, execute commands and retrieve results from database.
  • Context : name of the DbContext to create, default to database‚Äôs name
  • OutputDir : output of scaffolded entities (CLR types), folder will be created if not exists. If not specified, default is solution root.

Notes: error at several first attempts: The specified deps.json [C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\bin\Debug\netcoreapp3.1\DBFirst.deps.json] does not exist

Resolved after several attempts to run the scaffold command. (Try removing bin, obj folders)

3/ Update model

The same scaffolding command with -Force flag:

Scaffold-DbContext “Data Source=Your_Database_Name;Initial Catalog=AdventureWorks2017;Integrated Security=true;” -Provider Microsoft.EntityFrameworkCore.SqlServer -Context AdventureWorkContext -OutputDir ./Entities -Force

Notes: All entities will be overridden, any changes made to entities will be lost.

Leave a Reply

Your email address will not be published.

Please contact author for permission to reuse content