πŸ“‘ AddTypeCodeToWorkOrder Migration Documentation

File: IrbyImaging.DbContext/Migrations/20240805140229_AddTypeCodeToWorkOrder.cs
Namespace: IrbyImaging.DbContext.Migrations
Migration: AddTypeCodeToWorkOrder


πŸ“š Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Key Functional Areas
  4. Data Model Changes
  5. Migration Implementation Details
  6. UML Diagram
  7. Sample Commands & Usage Examples
  8. Conclusion

1. Overview & Purpose

This Entity Framework Core migration adds a new column named WorkOrderTypeCode to the existing WorkOrder table in the irby schema.

  • πŸ”§ Why?

    • To store a code representing the type of each work order (e.g., Repair, Inspection, Installation).
    • Enables filtering, reporting, and business logic based on work order types.
  • 🎯 When to Use?

    • After generating the migration, run dotnet ef database update (or Package Manager Console equivalent) to apply the schema change.
    • Update your C# domain model and any data access code to read/write this new column.

2. Dependencies & Technologies

Technology Version / Notes
.NET SDK β‰₯ 6.0 (or your project’s target framework)
Entity Framework Core β‰₯ 7.0 (for MigrationBuilder APIs)
C# 10.0+
Database SQL Server (nvarchar(max))

Tip: Ensure your project has a reference to Microsoft.EntityFrameworkCore.Design for migrations to scaffold correctly.


3. Key Functional Areas

  1. Schema Evolution
    • Introduces one new nullable column β†’ WorkOrderTypeCode.
  2. Reversibility
    • Implements Down(...) method to roll back the change (drop the column).

4. Data Model Changes

After applying this migration, your WorkOrder entity (in your DbContext) should include:

public class WorkOrder
{
    public int Id { get; set; }
    // ... existing properties ...

    /// <summary>
    /// Code representing the Work Order type (e.g., "INSPECT", "REPAIR").
    /// </summary>
    public string? WorkOrderTypeCode { get; set; }
}
Column Name Type Nullable Default Description
WorkOrderTypeCode nvarchar(max) βœ… Yes NULL Stores the type code for a work order.

5. Migration Implementation Details

using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace IrbyImaging.DbContext.Migrations
{
    /// <inheritdoc />
    public partial class AddTypeCodeToWorkOrder : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<string>(
                name: "WorkOrderTypeCode",
                schema: "irby",
                table: "WorkOrder",
                type: "nvarchar(max)",
                nullable: true);
        }

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

5.1 Up Method

  • Purpose: Adds the WorkOrderTypeCode column.
  • Key API Call:
    migrationBuilder.AddColumn<string>(
        name: "WorkOrderTypeCode",
        schema: "irby",
        table: "WorkOrder",
        type: "nvarchar(max)",
        nullable: true);
    

5.2 Down Method

  • Purpose: Reverts the schema to its previous state by dropping the column.
  • Key API Call:
    migrationBuilder.DropColumn(
        name: "WorkOrderTypeCode",
        schema: "irby",
        table: "WorkOrder");
    

6. UML Diagram

Below is a Mermaid class diagram showing the WorkOrder entity before and after the migration:

classDiagram
    class WorkOrder {
        +int Id
        +string? ExistingProperty1
        +DateTime CreatedDate
        +string? WorkOrderTypeCode   <<added>>
    }

    %% Schema: irby.WorkOrder

7. Sample Commands & Usage Examples

7.1 Applying the Migration

  1. Via .NET CLI

    dotnet ef migrations add AddTypeCodeToWorkOrder
    dotnet ef database update
    
  2. Via Package Manager Console (Visual Studio)

    Add-Migration AddTypeCodeToWorkOrder
    Update-Database
    

7.2 Reading / Writing the New Column

using var context = new IrbyDbContext();

// Create a new work order
var wo = new WorkOrder {
    /* other properties */,
    WorkOrderTypeCode = "REPAIR"
};
context.WorkOrders.Add(wo);
context.SaveChanges();

// Query by type code
var repairs = context.WorkOrders
    .Where(w => w.WorkOrderTypeCode == "REPAIR")
    .ToList();

8. Conclusion

✨ AddTypeCodeToWorkOrder is a straightforward, reversible EF Core migration that enhances the WorkOrder table with a new WorkOrderTypeCode column.

  • Ensures backwards compatibility via the Down method.
  • Provides flexibility for future business logic around work order types.

Next Steps:

  1. Update your domain models and DTOs/ViewModels.
  2. Adjust repository or service layer methods if filtering by work order type.
  3. Deploy migration to staging/production to begin capturing type codes in new records.

Happy coding! πŸš€