As we know, EF Core does not support a visual designer for the DB model and wizard to create the entity and context classes. So, we need to do Reverse engineering, which is the process of scaffolding entity type classes and a DbContext class based on a database schema. This reverse engineering command creates entity and context classes (by deriving DbContext) based on the schema of the existing database. It also includes the relationships(i.e. foreign key, primary key) between the entities.
It can be performed using the Scaffold-DbContext command of the EF Core Package Manager Console (PMC) tools or the dotnet ef DBContext scaffold command of the .NET Command-line Interface (CLI) tools.
The command for Scaffold:
Scaffold-DbContext -StartupProject 'Startup Project Name' -Project 'Project Name for generating entities'-Connection Name='Connection string name'
Microsoft.EntityFrameworkCore.SqlServer
-OutputDir 'Folder Name where the entities will store'
-Context 'Context File Name'
-Force -DataAnnotation -UseDatabaseNames
Let's Start with an Example:
First, create an ASP.Net core web application named 'EmployeeApp'. Then open your appsettings.json file and add your connection string inside the 'ConnectionStrings' Section.
0 Comments