Chapter 3
Chapter 3
Practice
02 Create Model
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
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)
{
}
www.usual.edu.lb 46
Migrate Model to Database
www.usual.edu.lb 47
Migrate Model to Database
www.usual.edu.lb 48
Migrate Model to Database
3. Database Changes
appears in the SQL
Server Explorer
www.usual.edu.lb 49
Created Controller with Views
www.usual.edu.lb 50
Created Controller with Views
• Generated Controller
• Generated Views
www.usual.edu.lb 51
Database Tables
www.usual.edu.lb 52
AI Prompt
Ask the AI Agent: