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