πŸ—‚οΈ RemoveDocImageFKToWOTable Migration Documentation

Filename:
IrbyImaging.DbContext/Migrations/20240301143253_RemoveDocImageFKToWOTable.cs


πŸ“‘ Table of Contents

  1. Overview & Purpose
  2. Key Functional Areas
  3. Dependencies & Technologies
  4. Migration Flow UML Diagram
  5. Code Walkthrough
  6. Usage Examples
  7. Error Handling & Return Types
  8. API Endpoints & View Models
  9. Third‑party Integrations
  10. Conclusion

1. Overview & Purpose

🎯 Purpose
This Entity Framework Core (EF Core) migration removes the foreign key relationship and associated column DocumentImageId from the WorkOrder table. It ensures that the schema no longer requires or enforces a link between WorkOrder records and DocumentImage entities.

🧩 Context

  • Project: IrbyImaging
  • Schema: irby
  • Affected Tables:
    • WorkOrder
    • DocumentImage

2. Key Functional Areas

Area Description
Up Method Drops FK constraint, index, and column from WorkOrder.
Down Method Reverses changes: re-adds column, index, and FK to restore original state.

Up Method

  • Drop Foreign Key
  • Drop Index
  • Drop Column

Down Method

  • Add Column
  • Create Index
  • Add Foreign Key

3. Dependencies & Technologies

  • Language: C#
  • Platform: .NET 7+
  • ORM: Entity Framework Core
  • NuGet Packages:
    • Microsoft.EntityFrameworkCore
    • Microsoft.EntityFrameworkCore.Relational

4. Migration Flow UML Diagram

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ RemoveDocImageFK β”‚          β”‚ MigrationBuilder  β”‚
β”‚  ToWOTable       │─────────▢│                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        β–²    β–Ό
        β”‚    β”‚
   Up() β”‚    β”‚ Down()
        β”‚    β”‚
        β–Ό    β–²
  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚ WorkOrder (irby schema)   β”‚
  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

5. Code Walkthrough

using System;
using Microsoft.EntityFrameworkCore.Migrations;

namespace IrbyImaging.DbContext.Migrations
{
    public partial class RemoveDocImageFKToWOTable : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            // 1. Drop FK constraint
            migrationBuilder.DropForeignKey(
                name: "FK_WorkOrder_DocumentImage_DocumentImageId",
                schema: "irby",
                table: "WorkOrder");

            // 2. Drop Index on DocumentImageId
            migrationBuilder.DropIndex(
                name: "IX_WorkOrder_DocumentImageId",
                schema: "irby",
                table: "WorkOrder");

            // 3. Drop the column DocumentImageId
            migrationBuilder.DropColumn(
                name: "DocumentImageId",
                schema: "irby",
                table: "WorkOrder");
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            // 1. Re-add the column
            migrationBuilder.AddColumn<Guid>(
                name: "DocumentImageId",
                schema: "irby",
                table: "WorkOrder",
                type: "uniqueidentifier",
                nullable: true);

            // 2. Re-create the index
            migrationBuilder.CreateIndex(
                name: "IX_WorkOrder_DocumentImageId",
                schema: "irby",
                table: "WorkOrder",
                column: "DocumentImageId");

            // 3. Re-establish the FK relationship
            migrationBuilder.AddForeignKey(
                name: "FK_WorkOrder_DocumentImage_DocumentImageId",
                schema: "irby",
                table: "WorkOrder",
                column: "DocumentImageId",
                principalSchema: "irby",
                principalTable: "DocumentImage",
                principalColumn: "DocumentImageId");
        }
    }
}

5.1 Up Method Details

  1. DropForeignKey

    • Purpose: Remove referential integrity check linking WorkOrder.DocumentImageId β†’ DocumentImage.DocumentImageId.
    • Effect: Prevents cascade-deletes or enforcement of relationship.
  2. DropIndex

    • Purpose: Remove the supporting index IX_WorkOrder_DocumentImageId.
    • Effect: Improves performance of FK lookupsβ€”must be dropped first.
  3. DropColumn

    • Purpose: Physically remove the DocumentImageId column.
    • Effect: WorkOrder table no longer stores any reference to DocumentImage.

5.2 Down Method Details

  1. AddColumn<Guid>

    • Re-introduce the DocumentImageId column of type uniqueidentifier, nullable.
  2. CreateIndex

    • Recreate the non-unique index on the new column.
  3. AddForeignKey

    • Revert referential integrity: links back to irby.DocumentImage.

6. Usage Examples

Applying the Migration

# 1. Add the migration to your project (already named):
dotnet ef migrations add RemoveDocImageFKToWOTable

# 2. Update the database:
dotnet ef database update

Rolling Back

# Rollback to prior migration
dotnet ef database update <PreviousMigrationName>

7. Error Handling & Return Types

  • EF Core Exceptions:
    • SqlException if the column/index/FK does not exist.
    • InvalidOperationException if attempted on a missing table/schema.
  • Return Type:
    Both Up and Down are void. Migrations are invoked by the EF runtime.

8. API Endpoints & View Models

πŸ”’ Not Applicable
This is strictly a database schema migrationβ€”no HTTP endpoints or view models are defined.


9. Third‑party Integrations

  • None
    No external services or packages beyond EF Core are used.

10. Conclusion

βœ… RemoveDocImageFKToWOTable is a simple yet crucial migration to drop an outdated foreign key relationship between WorkOrder and DocumentImage.

  • Use Up to apply the change, and Down to revert.
  • Always backup your database and test migrations in a staging environment before production deployment.

Happy Coding! πŸš€