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

Scaffold Identity Pages in ASP.NET MVC

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

Scaffold Identity Pages in ASP.NET MVC

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

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

[Link]
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

[Link] 43
Scaffold Identity Pages

• These are Identity scaffolded Pages

[Link] 44
Create Model
• Create new Class in the Model Folder “[Link]”
• Implement its properties
• Id (Primary Key)
• Name

using [Link];

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

}
}

[Link] 45
Create Model
• Open the “[Link]” (Created With Identity)
• Add the new DbSet for Student

using [Link];
using [Link];
using [Link];

namespace [Link]
{
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}

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


}
}

[Link] 46
Migrate Model to Database

• Open Package Manager Console

[Link] 47
Migrate Model to Database

3. Migration File Contents


2. Migration File Created

1. Run “Add-Migration MyMigration”

[Link] 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

[Link] 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

[Link] 50
Created Controller with Views

• Generated Controller
• Generated Views

[Link] 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”

[Link] 52
AI Prompt
Ask the AI Agent:

Explain the following C# code:


[Paste the code generated in the [Link]]
Discussion

You might also like