[go: up one dir, main page]

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

ASP.NET Core: EF Core Database First

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)
339 views5 pages

ASP.NET Core: EF Core Database First

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
You are on page 1/ 5

ENTITY FRAMEWORK CORE

(DATABASE FIRST)
ASP.NET 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 ASP.NET 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"
Microsoft.EntityFrameworkCore.SqlServer -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"
Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

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


Catalog=SCBuildersWebsiteDBV2;Integrated Security=True"
Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models –Context TestContext

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


Catalog=SCBuildersWebsiteDBV2;Integrated Security=True"
Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -DataAnnotations

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


Catalog=TestDBEFCore2;Integrated Security=True"
Microsoft.EntityFrameworkCore.SqlServer -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"
Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Force

Page | 4

You might also like