0% found this document useful (0 votes)
122 views5 pages

EF Core Database First Tutorial

This document provides steps for using Entity Framework Core with an existing database (database first approach) in an ASP.NET Core application. It outlines adding required packages, using Scaffold-DbContext to generate entity models from the database, and how to update the models and database if changes are made.

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
0% found this document useful (0 votes)
122 views5 pages

EF Core Database First Tutorial

This document provides steps for using Entity Framework Core with an existing database (database first approach) in an ASP.NET Core application. It outlines adding required packages, using Scaffold-DbContext to generate entity models from the database, and how to update the models and database if changes are made.

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
  • Introduction
  • Creating a New Project
  • Executing Commands
  • Adding Required Packages
  • Project Structure Overview
  • Other Migrations

ENTITY FRAMEWORK CORE

(DATABASE FIRST)
[Link] Core 7.0 or later

25 JULY 2023
Entity Framework Core Tutorial 25 July 2023

Entity Framework Core – Database First

Step #1: Create a New [Link] Core Web Application in Visual Studio 2019.

Page | 1
Entity Framework Core Tutorial 25 July 2023

Step #2: Add the required Packages as shown below.

Step #3: In Project Manager Console and add the following command
PM> Scaffold-DbContext "Data Source=DESKTOP-6AVBNVP\SQLEXPRESS;Initial
Catalog=SCBuildersWebsiteDBV2;Integrated Security=True"
[Link] -OutputDir Models

This will all the Generate Models in Models folders as shown below.

Page | 2
Entity Framework Core Tutorial 25 July 2023

Page | 3
Entity Framework Core Tutorial 25 July 2023

Other Migrations

PM> Scaffold-DbContext "Data Source=DESKTOP-6AVBNVP\SQLEXPRESS;Initial


Catalog=SCBuildersWebsiteDBV2;Integrated Security=True"
[Link] -OutputDir Models

PM> Scaffold-DbContext "Data Source=DESKTOP-6AVBNVP\SQLEXPRESS;Initial


Catalog=SCBuildersWebsiteDBV2;Integrated Security=True"
[Link] -OutputDir Models –Context TestContext

PM> Scaffold-DbContext "Data Source=DESKTOP-6AVBNVP\SQLEXPRESS;Initial


Catalog=SCBuildersWebsiteDBV2;Integrated Security=True"
[Link] -OutputDir Models -DataAnnotations

PM> Scaffold-DbContext "Data Source=DESKTOP-6AVBNVP\SQLEXPRESS;Initial


Catalog=TestDBEFCore2;Integrated Security=True"
[Link] -OutputDir Models/EF -Context TestDbContext -
DataAnnotations (Models/EF – Will store in EF Folder Inside Models Folder)

To Update Database Changes

PM> Scaffold-DbContext "Data Source=DESKTOP-6AVBNVP\SQLEXPRESS;Initial


Catalog=SCBuildersWebsiteDBV2;Integrated Security=True"
[Link] -OutputDir Models -Force

Page | 4

ENTITY FRAMEWORK CORE
(DATABASE FIRST)
ASP.NET Core 7.0 or later
25 JULY 2023
Entity Framework Core Tutorial
25 July 2023
Step #1: Create a New ASP.NET Core Web Application in Visual Studio 2019.
Page |
Entity Framework Core Tutorial
25 July 2023
Step #2: Add the required Packages as shown below.
Step #3: In Project Manager Co
Entity Framework Core Tutorial
25 July 2023
Page | 3
Entity Framework Core Tutorial
25 July 2023
Other Migrations
PM> Scaffold-DbContext "Data Source=DESKTOP-6AVBNVPSQLEXPRESS;I

You might also like