100% found this document useful (1 vote)
591 views13 pages

Entity Framework Core - Code First Steps

The document provides a tutorial on using Entity Framework Core, an object-relational mapping framework. It discusses what Entity Framework Core is, the code first and database first approaches, and provides steps to create a sample project using Entity Framework Core with code first development. These include adding entity classes, a context class, connection string, migrations, and updating the database.

Uploaded by

Javed Iqbal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
591 views13 pages

Entity Framework Core - Code First Steps

The document provides a tutorial on using Entity Framework Core, an object-relational mapping framework. It discusses what Entity Framework Core is, the code first and database first approaches, and provides steps to create a sample project using Entity Framework Core with code first development. These include adding entity classes, a context class, connection string, migrations, and updating the database.

Uploaded by

Javed Iqbal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

ENTITY FRAMEWORK CORE

ASP.NET Core 6.0

25 JULY 2023
Entity Framework Core Tutorial 25 July 2023

 What is Entity Framework Core:


Entity Framework Core
 Entity Framework Core is Object/Relational Mapping (O/RM) Framework.

1) What is Entity Framework Core?

Entity Framework Core is Object/Relational Mapping (O/RM) Framework.

2) What is O/RM Framework:

It is a Framework, which automatically generate for us

 Database, Tables & Stored Procedures


 Data Access Code
 Data Saving/Storage Code

3) What are Entity Framework Core Approaches?

 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.

Code First Approach:

 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.

Database First Approach:

 We Database First Approach, when we already have database.


 In this approach, Entity Framework generates the context class and entities classes from
our existing Database.

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

Entity Framework Core – Code First

System Requirements: Visual Studio 2019

 Step #1: Create a New ASP.NET Core Web Application in Visual Studio 2019.

 Step #2: Add the following Packages to your application


o Microsoft.EntityFrameworkCore
o Microsoft.EntityFrameworkCore.SqlServer
o Microsoft.EntityFrameworkCore.Tools

 Step #3: Add your Entity classes

 Step #4: Add Context Class

 Step #5: Add Connection String to your appsettings.json

 Step #6: Add Migration and Update Database

 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 #2: Add the following Packages as shown below.

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

Step #4: Add the context class as shown below …

Step #5: Add Connection String to your appsettings.json

Step #6: To add Migration and Update Database


PM> Add-Migration "InitialCreate" (To Create Initial Database & Table(s))

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

cmdlet Add-Migration at command pipeline position 1


Supply values for the following parameters:
Name: Employee (To give the migration name)

PM> Update-Database

Page | 6
Entity Framework Core Tutorial 25 July 2023

System Requirements: Visual Studio 2022

 Step #1: Create a New ASP.NET Core Web Application in Visual Studio 2019.

 Step #2: Add the following Packages to your application


o Microsoft.EntityFrameworkCore
o Microsoft.EntityFrameworkCore.SqlServer
o Microsoft.EntityFrameworkCore.Tools

 Step #3: Add your Entity classes

 Step #4: Add Context Class

 Step #5: Add Connection String to your appsettings.json

 Step #6: Add Migration and Update Database

 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 #2: Add the following Packages as shown below.

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

Step #4: Add the context class as shown below …

Step #5: Add Connection String to your appsettings.json

Step #6: To add Migration and Update Database


PM> Add-Migration "InitialCreate" (To Create Initial Database & Table(s))

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

cmdlet Add-Migration at command pipeline position 1


Supply values for the following parameters:
Name: Employee (To give the migration name)

PM> Update-Database

Page | 11
Entity Framework Core Tutorial 25 July 2023

About Migration Commands


 Initial Migration:
o Add-Migration “InitialCreate”
o Update-Database

 Remove Migration:
o Remove-Migration (Before Update-Database)
o Remove-Migration -force (After Update-Database)

 To Remove All Migration:


o Update-Database -Migration:0

To Add A New Column(s) to your existing Table

Step #1: Add a New Property to your Entity Class.

Step #2: Add-Migration


Name: “Give Your Migration Name”

Step #3: Update-Database

Page | 12

You might also like