📂 IrbyImaging.DbContext Migrations

20240307125652_ChangeJobIdToNullable


Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Key Functional Areas
  4. EF Core Migration Structure
  5. Schema Before & After Change
  6. UML Diagram
  7. Usage Examples
  8. Conclusion

Overview & Purpose

File: IrbyImaging.DbContext/Migrations/20240307125652_ChangeJobIdToNullable.cs

This EF Core migration adjusts the Equipment table's JobId foreign‑key column to be nullable, allowing records of equipment that are not yet assigned to a job.

  • ✏️ Why?
    • In the original schema, every piece of equipment must reference a Job (non-nullable).
    • Business scenarios emerged where equipment can exist without initially being associated with a job (e.g., newly procured or under maintenance).

Dependencies & Technologies

Category Details
Framework .NET 7.0 / .NET Core
ORM Entity Framework Core (Microsoft.EntityFrameworkCore.Migrations)
Database SQL Server (uniqueidentifier type)
Schema irby

Key Functional Areas

  • 🚧 Dropping Existing FK
  • ✨ Altering Column Nullability
  • 🔌 Re‑adding Foreign Key Constraint
  • ↩️ Reversible Migration (via Down method)

EF Core Migration Structure

All migrations inherit from Migration and define two methods:

  1. Up: Applies schema changes.
  2. Down: Reverts changes back to the previous state.
public partial class ChangeJobIdToNullable : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder) { /* … */ }
    protected override void Down(MigrationBuilder migrationBuilder) { /* … */ }
}

Up Method

protected override void Up(MigrationBuilder migrationBuilder)
{
    // 1️⃣ Drop existing foreign key
    migrationBuilder.DropForeignKey(
        name: "FK_Equipment_Job_JobId",
        schema: "irby",
        table: "Equipment");

    // 2️⃣ Alter the JobId column to nullable
    migrationBuilder.AlterColumn<Guid>(
        name: "JobId",
        schema: "irby",
        table: "Equipment",
        type: "uniqueidentifier",
        nullable: true,               // ← Change here
        oldClrType: typeof(Guid),
        oldType: "uniqueidentifier");

    // 3️⃣ Re-add foreign key without cascade delete
    migrationBuilder.AddForeignKey(
        name: "FK_Equipment_Job_JobId",
        schema: "irby",
        table: "Equipment",
        column: "JobId",
        principalSchema: "irby",
        principalTable: "Job",
        principalColumn: "JobId");
}

Highlights

  • nullable: true makes the column accept NULL.
  • The FK is re‑added without a cascade delete (onDelete: ReferentialAction.NoAction by default).

Down Method

protected override void Down(MigrationBuilder migrationBuilder)
{
    // 1️⃣ Drop newly added foreign key
    migrationBuilder.DropForeignKey(
        name: "FK_Equipment_Job_JobId",
        schema: "irby",
        table: "Equipment");

    // 2️⃣ Revert the JobId column to non-nullable, defaulting to empty GUID
    migrationBuilder.AlterColumn<Guid>(
        name: "JobId",
        schema: "irby",
        table: "Equipment",
        type: "uniqueidentifier",
        nullable: false,              // ← Back to required
        defaultValue: new Guid("00000000-0000-0000-0000-000000000000"),
        oldClrType: typeof(Guid),
        oldType: "uniqueidentifier",
        oldNullable: true);

    // 3️⃣ Re-add foreign key with cascade delete
    migrationBuilder.AddForeignKey(
        name: "FK_Equipment_Job_JobId",
        schema: "irby",
        table: "Equipment",
        column: "JobId",
        principalSchema: "irby",
        principalTable: "Job",
        principalColumn: "JobId",
        onDelete: ReferentialAction.Cascade);
}

Highlights

  • Resets JobId to non-nullable with a default value of 00000000-0000-0000-0000-000000000000.
  • Re‑introduces cascade delete: deleting a Job will remove related Equipment.

Schema Before & After Change

Aspect Before After
Column Nullability JobId UNIQUEIDENTIFIER NOT NULL JobId UNIQUEIDENTIFIER NULL
FK Constraint ON DELETE CASCADE ON DELETE NO ACTION (no cascade)
Default Value N/A N/A (defaults to NULL)

UML Diagram

+-----------------------------------------+
|        ChangeJobIdToNullable            |
+-----------------------------------------+
| <<inherits>> Migration                  |
+-----------------------------------------+
| + Up(migrationBuilder: MigrationBuilder)|
| + Down(migrationBuilder: MigrationBuilder)|
+-----------------------------------------+

Usage Examples

  1. Add Migration

    dotnet ef migrations add ChangeJobIdToNullable
    
  2. Apply Migration

    dotnet ef database update
    
  3. Revert (if needed)

    dotnet ef database update 20240307125651_PreviousMigration
    

Conclusion

This migration offers a seamless transition to allow Equipment records to exist without a mandatory Job association, improving flexibility in handling newly procured or unassigned equipment. The reversible design ensures you can roll back safely if requirements change. 🎉


Generated by the IrbyImaging documentation generator.