π AddPWDColumnsToEmployeeTable Migration Documentation
π Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Migration Structure & Mechanics
- Code Walkthrough
- 4.1 Up Method
- 4.2 Down Method
- Table Schema Evolution
- Usage Examples
- Rolling Back the Migration
- UML Diagram
- 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:
- Up: Defines schema changes to apply when updating the database.
- 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
- name:
-
AddColumn<DateTime>
- name:
"PWResetDate" - schema:
"irby" - table:
"Employee" - type:
"datetime2" - nullable:
false - defaultValue:
DateTime.MinValue
- name:
Note: Setting a non-nullable column requires a default value to backfill existing rows.
4.2 Down Method
- DropColumn calls remove
PWResetandPWResetDate, restoring the previous schema.
π Table Schema Evolution
Before Migration
| Column | Type | Nullable |
|---|---|---|
| EmployeeId (PK) | int | No |
| Name | nvarchar(50) | No |
| nvarchar(100) | No | |
| ... | ... | ... |
After Migration
| Column | Type | Nullable | Default Value |
|---|---|---|---|
| EmployeeId (PK) | int | No | β |
| Name | nvarchar(50) | No | β |
| nvarchar(100) | No | β | |
| PWReset | bit | No | false |
| PWResetDate | datetime2 | No | 0001-01-01T00:00:00Z |
π Usage Examples
-
Generate Migration
dotnet ef migrations add AddPWDColumnsToEmployeeTable -c IrbyDbContext -
Apply Migration to Database
dotnet ef database update -
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! π