PRACTICAL 10: ENTITY FRAMEWORK CORE SETUP AND MIGRATION

10.1 EF CORE SETUP AND MIGRATION

STEP 1: Create Project
 - Open Visual Studio.
 - Create a new project -> ASP.NET Core Web Application.
 - Select ASP.NET Core 3.0 -> choose Web Application (Model-View-Controller).
 - Name it EFCoreDemo.

STEP 2: Install Required EF Core Packages
Open Package Manager Console (PMC)
(Tools -> NuGet Package Manager -> Package Manager Console) and run:
Install-Package Microsoft.EntityFrameworkCore -Version 3.0.0
Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 3.0.0
Install-Package Microsoft.EntityFrameworkCore.Tools -Version 3.0.0
Install-Package Microsoft.EntityFrameworkCore.Design -Version 3.0.0

STEP 3: Create Your Model
In Models folder -> Add new class -> Student.cs:
namespace EFCoreDemo.Models
{
    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

STEP 4: Create DbContext
In Data folder -> Add new class -> AppDbContext.cs:
using Microsoft.EntityFrameworkCore;
using EFCoreDemo.Models;

namespace EFCoreDemo.Data
{
    public class AppDbContext : DbContext
    {
        public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }

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

STEP 5: Configure Connection String
Open appsettings.json:
{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=EFCoreDemoDB;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

STEP 6: Register DbContext in Startup.cs
Open Startup.cs -> inside ConfigureServices method:
using EFCoreDemo.Data;
using Microsoft.EntityFrameworkCore;

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<AppDbContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

    services.AddControllersWithViews();
}

STEP 7: Add Migration
In Package Manager Console:
Add-Migration InitialCreate

STEP 8: Apply Migration to Database
In Package Manager Console:
Update-Database
This creates the EFCoreDemoDB database with a Students table.

STEP 9: Verify
Open SQL Server Object Explorer in Visual Studio -> expand (localdb)\MSSQLLocalDB ->
check for database EFCoreDemoDB -> verify that the Students table exists.


10.2 CRUD OPERATIONS USING EF CORE

STEP 1: Scaffold a Controller with Views
 - Right-click the Controllers folder -> Add -> New Scaffolded Item.
 - Choose "MVC Controller with views, using Entity Framework".
 - Select:
     Model class        -> Student (EFCoreDemo.Models)
     Data context class -> AppDbContext (EFCoreDemo.Data)
     Name it StudentsController.
 - Click Add.
This will auto-generate:
 - StudentsController.cs in Controllers
 - Views for Create, Edit, Delete, Details, Index inside Views/Students

STEP 2: Run and Test
Navigate to /Students in the browser. You will see a list page (Index) where you can:
 - Create a new student
 - Edit an existing student
 - Delete a student
 - View Details

TO ADD ANNOTATIONS: (Models/Student.cs)
using System.ComponentModel.DataAnnotations;

public class Student
{
    [Key]
    public int Id { get; set; }

    [Required]
    [StringLength(50)]
    public string Name { get; set; }

    [Range(18, 60)]
    public int Age { get; set; }
}

OUTPUT:
Index (list), Create, Edit, Delete, Details pages and the list after a delete.
With the annotations, an empty Name or an Age outside 18-60 shows a validation error.
