Entity Framework Core - Code First Steps
Entity Framework Core - Code First Steps
25 JULY 2023
Entity Framework Core Tutorial 25 July 2023
Code First
Database First
Note: The configuration of Code First & Database First Approaches is different but the code such
Database Query, CRUD Operations & other code of both approaches is the same.
We use Code First Approach, when we don’t have Database or don’t want to use existing
Database.
In this approach, Entity Framework generates the Database from our context class and
Entities/Model classes.
Context Class:
The context class is used to query or do CRUD operations or any other operations to our
database.
The context class should be derived from DbContext class.
Page | 1
Entity Framework Core Tutorial 25 July 2023
Step #1: Create a New ASP.NET Core Web Application in Visual Studio 2019.
Step #7: To Add New Class and to generate Table(s) in the Database
Step #1: Create a New ASP.NET Core Web Application in Visual Studio 2019.
Page | 2
Entity Framework Core Tutorial 25 July 2023
Page | 3
Entity Framework Core Tutorial 25 July 2023
Step #3: To create the required Table(s), add a Model for each
table as shown below.
Page | 4
Entity Framework Core Tutorial 25 July 2023
PM> Update-Database
Step #7: To add New Class and to generate new Table(s) in the Database, just
add a new Model.
Page | 5
Entity Framework Core Tutorial 25 July 2023
PM> Add-Migration
PM> Update-Database
Page | 6
Entity Framework Core Tutorial 25 July 2023
Step #1: Create a New ASP.NET Core Web Application in Visual Studio 2019.
Step #7: To Add New Class and to generate Table(s) in the Database
Step #1: Create a New ASP.NET Core Web Application in Visual Studio 2022.
Page | 7
Entity Framework Core Tutorial 25 July 2023
Page | 8
Entity Framework Core Tutorial 25 July 2023
Step #3: To create the required Table(s), add a Model for each
table as shown below.
Page | 9
Entity Framework Core Tutorial 25 July 2023
PM> Update-Database
Step #7: To add New Class and to generate new Table(s) in the Database, just
add a new Model.
Page | 10
Entity Framework Core Tutorial 25 July 2023
PM> Add-Migration
PM> Update-Database
Page | 11
Entity Framework Core Tutorial 25 July 2023
Remove Migration:
o Remove-Migration (Before Update-Database)
o Remove-Migration -force (After Update-Database)
Page | 12