0% found this document useful (0 votes)
4 views7 pages

EF Core Database First Approach

This document outlines the steps to create an ASP.NET Core MVC application using the Entity Framework Core Database First approach with an existing SQL Server database. It includes instructions for setting up the database, creating the MVC project, installing necessary packages, generating models, and configuring the application to display student data. The final output is a list of students displayed in a web view after running the project.

Uploaded by

Aastha Teotia
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)
4 views7 pages

EF Core Database First Approach

This document outlines the steps to create an ASP.NET Core MVC application using the Entity Framework Core Database First approach with an existing SQL Server database. It includes instructions for setting up the database, creating the MVC project, installing necessary packages, generating models, and configuring the application to display student data. The final output is a list of students displayed in a web view after running the project.

Uploaded by

Aastha Teotia
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

EF Core Database First Approach in [Link] Core MVC (.

NET 8)

Objective

Create an [Link] Core MVC application that reads data from an existing
SQL Server database using Entity Framework Core Database First
Approach.

STEP 1: Create SQL Server Database

Open SQL Server Management Studio (SSMS) and execute the following
SQL script.

CREATE DATABASE CollegeDB;

USE CollegeDB;

CREATE TABLE Student

Id INT PRIMARY KEY IDENTITY(1,1),

Name NVARCHAR(100),

Age INT,

City NVARCHAR(100)

);

INSERT INTO Student(Name,Age,City)

VALUES

('Rahul',21,'Delhi'),

('Priya',22,'Mumbai'),

('Amit',20,'Agra'),

('Neha',23,'Pune');

STEP 2: Create [Link] Core MVC Project

Visual Studio 2022

File → New → Project → [Link] Core Web App (Model-View-Controller)

Project Name:

DatabaseFirstDemo
Version:

.NET 8

Click Create.

STEP 3: Install Required NuGet Packages

Go to

Tools → NuGet Package Manager → Manage NuGet Packages

Install these packages

[Link]

[Link]

STEP 4: Generate Models from Database

Open

Tools → NuGet Package Manager → Package Manager Console

Run

Scaffold-DbContext
"Server=YOUR_SERVER_NAME;Database=CollegeDB;Trusted_Connection=
True;TrustServerCertificate=True;"
[Link] -OutputDir Models -Context
CollegeDbContext -Force

Example

Scaffold-DbContext "Server=DESKTOP-
ABC123;Database=CollegeDB;Trusted_Connection=True;TrustServerCertifi
cate=True;" [Link] -OutputDir Models -
Context CollegeDbContext -Force

If SQL Authentication is used

Scaffold-DbContext "Server=YOUR_SERVER;Database=CollegeDB;User
Id=sa;Password=yourpassword;TrustServerCertificate=True;"
[Link] -OutputDir Models -Context
CollegeDbContext -Force

STEP 5: Files Generated Automatically


Models folder

[Link]

[Link]

These files are generated automatically.

DO NOT create them manually.

STEP 6: Register DbContext

Open [Link]

Replace with

using [Link];

using [Link];

var builder = [Link](args);

[Link]();

[Link]<CollegeDbContext>(options =>

[Link]([Link]("DefaultC
onnection")));

var app = [Link]();

[Link]();

[Link]();

[Link](

name: "default",

pattern: "{controller=Student}/{action=Index}/{id?}");

[Link]();

STEP 7: Add Connection String

Open [Link]

"ConnectionStrings": {
"DefaultConnection":
"Server=YOUR_SERVER_NAME;Database=CollegeDB;Trusted_Connection=
True;TrustServerCertificate=True;"

},

"Logging": {

"LogLevel": {

"Default": "Information",

"[Link]": "Warning"

},

"AllowedHosts": "*"

STEP 8: Create Controller

Controllers → [Link]

using [Link];

using [Link];

namespace [Link]

public class StudentController : Controller

private readonly CollegeDbContext db;

public StudentController(CollegeDbContext context)

db = context;

public IActionResult Index()


{

var students = [Link]();

return View(students);

STEP 9: Create View

Views

Student

[Link]

@model IEnumerable<[Link]>

<h2>Student List</h2>

<table border="1" cellpadding="10">

<tr>

<th>Id</th>

<th>Name</th>

<th>Age</th>

<th>City</th>

</tr>

@foreach(var item in Model)

<tr>

<td>@[Link]</td>

<td>@[Link]</td>

<td>@[Link]</td>

<td>@[Link]</td>

</tr>
}

</table>

STEP 10: Run the Project

Press

Ctrl + F5

Output

Student List

---------------------------------

Id Name Age City

---------------------------------

1 Rahul 21 Delhi

2 Priya 22 Mumbai

3 Amit 20 Agra

4 Neha 23 Pune

---------------------------------

============================================
================ How Database First Works

Database Already Exists

Install EF Core Packages

Run Scaffold-DbContext Command

EF Core Generates

• Entity Classes • DbContext Class


Register DbContext

Inject DbContext into Controller

Read Data using LINQ

Send Data to View

Display Data

You might also like