Generate the Entity Model files in .net core

In this article we will see how we can generate the Entity model file in .Net core application. In .net  framework we are getting templates which help us to connect the database and generate the data model and edmx file. However in .net core this templates are missing.

Here we will see how we can generate the database (DbContext) filein .net Core using scaffolding.

What is scaffolding ?

Scaffolding is a code generation framework that automatically adds codes and creates view pages and controllers for your project. Scaffolding makes developer job easier by creating controllers and view pages for the data model. It generates codes and pages for CRUD(Create, Read, Update and Delete) Operation.


Provided below are steps for same

  1. Open the Visual Studio IDE & Open the Solutions
  2. Navigate to Tools, Nuget Package Manager & click on Package Manager Console.
  3. Note: Import the Microsoft.EntityFrameworkCore.SqlServer from Nuget Manager before we perform this steps
  4. Type the below mentioned command and hit enter
  5. Scaffold-DbContext "Server=[SQL Server Instance];database=[Database Name];integrated security=SSPI" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -force
    1. Note: integrated security=SSPI will be replace with SQL userId and Password if you are using SQL Authentication.
    2. force is optional, this command is use to refresh the DB Model.
  6. The above command will generate the DB Context & entity files and after successfully compilation our .net core application is ready to connect the DBB using LINQ Query.
Scaffold-DbContext "Server=[SQL Server Instance];database=[Database Name];integrated security=SSPI" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -force