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
- Open the Visual Studio IDE & Open the Solutions
- Navigate to Tools, Nuget Package Manager & click on Package Manager Console.
- Note: Import the Microsoft.EntityFrameworkCore.SqlServer from Nuget Manager before we perform this steps
- Type the below mentioned command and hit enter
- Scaffold-DbContext "Server=[SQL Server Instance];database=[Database Name];integrated security=SSPI" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -force
- Note: integrated security=SSPI will be replace with SQL userId and Password if you are using SQL Authentication.
- force is optional, this command is use to refresh the DB Model.
- 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