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

Chapter 3

Chapter 3 outlines the process of creating and scaffolding a custom model, specifically a 'Student' model, in an ASP.NET MVC project. It details steps for scaffolding identity pages, creating the model and its properties, migrating the model to the database, and generating a controller with views. The chapter also includes instructions for viewing the auto-generated database in SQL Server Object Explorer.

Uploaded by

aliawela00
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views14 pages

Chapter 3

Chapter 3 outlines the process of creating and scaffolding a custom model, specifically a 'Student' model, in an ASP.NET MVC project. It details steps for scaffolding identity pages, creating the model and its properties, migrating the model to the database, and generating a controller with views. The chapter also includes instructions for viewing the auto-generated database in SQL Server Object Explorer.

Uploaded by

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

Chapter 3

Practice

01 Scaffold Identity Pages 04 Scaffold Controller with


View

02 Create Model

03 Migrate Model to Database


Demo: Create 1st Model, Controller, Views
Will Create a Custom Model, Migrate it to the Database, and Create
Related Views.

01 After Creating the Project in Chapter 2 (With Identity),


Scaffold the Identity Pages

02 Create Model, Migrate it to the Database

03 Scaffold Controller with Views

www.usal.edu.lb
Scaffold Identity Pages

• Right click on the project -> Add -> New Scaffolded Item • Select Pages you want to override
• Select Identity • Select the “Existing” Application DbContext
• Add • Add

www.usual.edu.lb 43
Scaffold Identity Pages

• These are Identity scaffolded Pages

www.usual.edu.lb 44
Create Model
• Create new Class in the Model Folder “Student.cs”
• Implement its properties
• Id (Primary Key)
• Name

using System.ComponentModel.DataAnnotations;

namespace MyMVC.Models
{
public class Student
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }

}
}

www.usual.edu.lb 45
Create Model
• Open the “ApplicationDbContext.cs” (Created With Identity)
• Add the new DbSet for Student

using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using MyMVC.Models;

namespace MyMVC.Data
{
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}

public DbSet<Student> Students { get; set; }


}
}

www.usual.edu.lb 46
Migrate Model to Database

• Open Package Manager Console

www.usual.edu.lb 47
Migrate Model to Database

3. Migration File Contents


2. Migration File Created

1. Run “Add-Migration MyMigration”

www.usual.edu.lb 48
Migrate Model to Database

1. Run “Update- Database”

3. Database Changes
appears in the SQL
Server Explorer

2. SQL Scripts will


Automatically Executed in
the Database

www.usual.edu.lb 49
Created Controller with Views

• Right click on the project in Solution Explorer • Select the Model


• Add -> New Scaffolded Item • Select the “Existing” ApplicationDbContext
• Select MVC Controller with views using EF • Select Layout
• Add

www.usual.edu.lb 50
Created Controller with Views

• Generated Controller
• Generated Views

www.usual.edu.lb 51
Database Tables

Add SQL Server

To View the auto generated database (localdb)


• View -> SQL Server Object Explorer
• Expand the SQL Server
• Expand the (localdb)\MSSQLLocalDB
• Find your database in Databases Folder
Note: if you can find (localdb)\MSSQLLocalDB, Our SQL Database
Add it using “Add SQL Server”

www.usual.edu.lb 52
AI Prompt
Ask the AI Agent:

Explain the following C# code:


[Paste the code generated in the StudentController.cs]
Discussion

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy