πŸ“š AddPWDColumnsToEmployeeTable Migration Documentation


πŸ“‘ Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Migration Structure & Mechanics
  4. Code Walkthrough
  5. Table Schema Evolution
  6. Usage Examples
  7. Rolling Back the Migration
  8. UML Diagram
  9. Conclusion

🧐 Overview & Purpose

File:
IrbyImaging.DbContext/Migrations/20240131151348_AddPWDColumnsToEmployeeTable.cs

Purpose:
This Entity Framework Core migration adds two new columnsβ€”PWReset and PWResetDateβ€”to the irby.Employee table, enabling the application to track password reset status and the date of the last reset for each employee.

  • PWReset (bool)
    Indicates whether a password reset has been requested or performed.
  • PWResetDate (DateTime)
    Captures the timestamp when the password was last reset.

βš™οΈ Dependencies & Technologies

Technology Version/Type
.NET SDK β‰₯ .NET 6.0
Entity Framework Core β‰₯ EF Core 6.0
Microsoft.EntityFrameworkCore.Tools For running dotnet ef commands
Database Provider Microsoft SQL Server (datetime2, bit)

πŸ”§ Migration Structure & Mechanics

Entity Framework Core migrations are C# classes that inherit from Migration and override two methods:

  1. Up: Defines schema changes to apply when updating the database.
  2. Down: Reverts those changes when rolling back.

This particular migration lives under the IrbyImaging.DbContext.Migrations namespace and follows the convention:

public partial class AddPWDColumnsToEmployeeTable : Migration

πŸ“œ Code Walkthrough

using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace IrbyImaging.DbContext.Migrations
{
    /// <inheritdoc />
    public partial class AddPWDColumnsToEmployeeTable : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<bool>(
                name: "PWReset",
                schema: "irby",
                table: "Employee",
                type: "bit",
                nullable: false,
                defaultValue: false);

            migrationBuilder.AddColumn<DateTime>(
                name: "PWResetDate",
                schema: "irby",
                table: "Employee",
                type: "datetime2",
                nullable: false,
                defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified));
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "PWReset",
                schema: "irby",
                table: "Employee");

            migrationBuilder.DropColumn(
                name: "PWResetDate",
                schema: "irby",
                table: "Employee");
        }
    }
}

4.1 Up Method

  • AddColumn<bool>

    • name: "PWReset"
    • schema: "irby"
    • table: "Employee"
    • type: "bit"
    • nullable: false
    • defaultValue: false
  • AddColumn<DateTime>

    • name: "PWResetDate"
    • schema: "irby"
    • table: "Employee"
    • type: "datetime2"
    • nullable: false
    • defaultValue: DateTime.MinValue

Note: Setting a non-nullable column requires a default value to backfill existing rows.

4.2 Down Method

  • DropColumn calls remove PWReset and PWResetDate, restoring the previous schema.

πŸ”„ Table Schema Evolution

Before Migration

Column Type Nullable
EmployeeId (PK) int No
Name nvarchar(50) No
Email nvarchar(100) No
... ... ...

After Migration

Column Type Nullable Default Value
EmployeeId (PK) int No β€”
Name nvarchar(50) No β€”
Email nvarchar(100) No β€”
PWReset bit No false
PWResetDate datetime2 No 0001-01-01T00:00:00Z

πŸš€ Usage Examples

  1. Generate Migration

    dotnet ef migrations add AddPWDColumnsToEmployeeTable -c IrbyDbContext
    
  2. Apply Migration to Database

    dotnet ef database update
    
  3. Check Schema

    SELECT TOP 1 PWReset, PWResetDate
    FROM irby.Employee;
    

πŸ”™ Rolling Back the Migration

To revert this migration:

dotnet ef database update 20240131151347_PreviousMigrationName

This executes the Down method, dropping the two added columns.


πŸ“Š UML Diagram

Simple class relationship:

+--------------------------------------------+
|            Migration (EF Core)             |
+--------------------------------------------+
| + Up(migrationBuilder)                     |
| + Down(migrationBuilder)                   |
+--------------------------------------------+
            ^
            |
+--------------------------------------------+
| AddPWDColumnsToEmployeeTable : Migration   |
+--------------------------------------------+
| Overrides:                                 |
|  - void Up(MigrationBuilder)               |
|  - void Down(MigrationBuilder)             |
+--------------------------------------------+

🏁 Conclusion

This migration cleanly extends the irby.Employee table with password-reset tracking columns.
By following EF Core’s conventions and using default values, it ensures zero-downtime schema evolution. After applying, your application can record and query password reset events for auditing or security workflows.


Happy coding! πŸŽ‰