📋 Table of Contents

  1. Overview & Purpose
  2. Migration Class at a Glance
  3. Up() Method – Applying Changes
  4. Down() Method – Reverting Changes
  5. Dependencies & Technologies
  6. Entity Relationship Diagram (UML)
  7. Usage Examples
  8. Conclusion

1. Overview & Purpose 🚀

This Migration class, AddDocImageFKToWOTable, is part of the
IrbyImaging.DbContext.Migrations namespace and was generated on 2024-03-01 14:24:22.

What it does

  • Adds a nullable foreign‐key column DocumentImageId to the WorkOrder table.
  • Creates an index on that column for performance.
  • Establishes a constraint linking WorkOrder.DocumentImageId → DocumentImage.DocumentImageId.

Why it exists

  • To allow each WorkOrder to optionally reference a related DocumentImage.
  • This supports scenarios where an image (e.g., a scanned invoice, proof‐of‐service photo) is attached directly to a work order record.

2. Migration Class at a Glance 🛠️

namespace IrbyImaging.DbContext.Migrations
{
    public partial class AddDocImageFKToWOTable : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder) { /* … */ }
        protected override void Down(MigrationBuilder migrationBuilder) { /* … */ }
    }
}
Class / Method Responsibility
AddDocImageFKToWOTable Defines the schema change operations
Up(...) Executes schema changes to apply migration
Down(...) Reverts the changes to undo the migration

3. Up() Method – Applying Changes ⚙️

When you run dotnet ef database update, the Up() method is executed.

protected override void Up(MigrationBuilder migrationBuilder)
{
    // 1) Add the FK column to WorkOrder (nullable Guid)
    migrationBuilder.AddColumn<Guid>(
        name: "DocumentImageId",
        schema: "irby",
        table: "WorkOrder",
        type: "uniqueidentifier",
        nullable: true);

    // 2) Create an index to speed up FK lookups
    migrationBuilder.CreateIndex(
        name: "IX_WorkOrder_DocumentImageId",
        schema: "irby",
        table: "WorkOrder",
        column: "DocumentImageId");

    // 3) Add the foreign key constraint
    migrationBuilder.AddForeignKey(
        name: "FK_WorkOrder_DocumentImage_DocumentImageId",
        schema: "irby",
        table: "WorkOrder",
        column: "DocumentImageId",
        principalSchema: "irby",
        principalTable: "DocumentImage",
        principalColumn: "DocumentImageId");
}

Step-by-Step

  1. AddColumn – Introduces DocumentImageId of type uniqueidentifier (SQL Server’s GUID) to irby.WorkOrder.
  2. CreateIndex – Builds IX_WorkOrder_DocumentImageId for faster lookups and JOINs.
  3. AddForeignKey – Enforces referential integrity linking to irby.DocumentImage.

4. Down() Method – Reverting Changes 🔄

If you roll back this migration (e.g., dotnet ef database update <previous_migration>), Down() undoes each step:

protected override void Down(MigrationBuilder migrationBuilder)
{
    migrationBuilder.DropForeignKey(
        name: "FK_WorkOrder_DocumentImage_DocumentImageId",
        schema: "irby",
        table: "WorkOrder");

    migrationBuilder.DropIndex(
        name: "IX_WorkOrder_DocumentImageId",
        schema: "irby",
        table: "WorkOrder");

    migrationBuilder.DropColumn(
        name: "DocumentImageId",
        schema: "irby",
        table: "WorkOrder");
}

Reversion Steps

  1. DropForeignKey – Removes the FK constraint.
  2. DropIndex – Drops the index on DocumentImageId.
  3. DropColumn – Deletes the DocumentImageId column from WorkOrder.

5. Dependencies & Technologies 🧩

Technology Version / Details
.NET / .NET Core 6.0 / 7.0 (EF Core)
Entity Framework Core Migrations Microsoft.EntityFrameworkCore.Migrations
Database SQL Server (uniqueidentifier column)
Schema "irby" – custom DB schema prefix

No external third‐party integrations are required by this migration itself. It leverages built‐in EF Core capabilities.


6. Entity Relationship Diagram (UML) 📊

erDiagram
    DOCUMENTIMAGE {
        Guid DocumentImageId PK
        ... other fields ...
    }
    WORKORDER {
        Guid WorkOrderId PK
        Guid? DocumentImageId FK
        ... other fields ...
    }

    WORKORDER }|..|{ DOCUMENTIMAGE : "references"
  • 🎯 One-to-Many-ish (optional):
    A WorkOrder may have zero or one associated DocumentImage.

7. Usage Examples 💡

a) Applying the Migration

# In your project directory:
dotnet ef migrations add AddDocImageFKToWOTable
dotnet ef database update

b) Data Model Snippet

Update your WorkOrder entity class:

public class WorkOrder
{
    public Guid WorkOrderId { get; set; }
    // New navigation property:
    public Guid? DocumentImageId { get; set; }
    public DocumentImage? DocumentImage { get; set; }
}

c) Querying with EF Core

// Eager loading the related image:
var workOrder = await dbContext.WorkOrders
    .Include(wo => wo.DocumentImage)
    .FirstOrDefaultAsync(wo => wo.WorkOrderId == someId);

if (workOrder?.DocumentImage != null)
{
    Console.WriteLine($"Image found: {workOrder.DocumentImage.FileName}");
}

8. Conclusion ✅

The AddDocImageFKToWOTable migration cleanly extends the WorkOrder schema by adding an optional foreign‐key to the DocumentImage table.

  • Ensures referential integrity 🛡️
  • Improves query performance via an index ⚡
  • Simplifies navigation in your EF Core model

With this in place, your application can now attach, query, and manage images (invoices, photos, etc.) directly in the context of a work order—enhancing traceability and data modeling consistency.