πŸ“š AddWorkOrderType Migration Documentation


πŸ“‘ Table of Contents

  1. 🎯 Overview & Purpose
  2. πŸ› οΈ Dependencies & Technologies
  3. πŸ” Entity Framework Core Migrations Primer
  4. 🚧 Migration File: AddWorkOrderType
  5. 🧩 Data Model Changes
  6. πŸš€ Applying the Migration
  7. πŸ”„ Rolling Back the Migration
  8. πŸ”— Integration with DbContext
  9. ⚠️ Error Handling & Considerations
  10. πŸŽ‰ Conclusion

🎯 Overview & Purpose

The AddWorkOrderType migration introduces a new column, WorkOrderType, to the MechanicTimesheet table in the irby schema.
This enhancement allows tracking of the type/category of work order each mechanic records, improving reporting and filtering capabilities.


πŸ› οΈ Dependencies & Technologies

Technology Version Purpose
.NET SDK 6.0 / 7.0+ Application platform
Microsoft.EntityFrameworkCore 6.0 / 7.0+ ORM & migrations
Microsoft.EntityFrameworkCore.SqlServer 6.0 / 7.0+ SQL Server provider
Microsoft.EntityFrameworkCore.Tools 6.0 / 7.0+ CLI tooling for EF Core

πŸ” Entity Framework Core Migrations Primer

Entity Framework Core Migrations let you incrementally update your database schema to keep it in sync with your data model. Each migration class:

  • Inherits from Migration.
  • Implements an Up method to apply schema changes.
  • Implements a Down method to revert them.

🚧 Migration File: AddWorkOrderType

Location:

IrbyImaging.DbContext/Migrations/20240801143541_AddWorkOrderType.cs
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

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

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

πŸ“‹ UML Class Diagram

classDiagram
    Migration <|-- AddWorkOrderType
    class Migration {
      <<abstract>>
      +Up(migrationBuilder: MigrationBuilder)
      +Down(migrationBuilder: MigrationBuilder)
    }
    class AddWorkOrderType {
      +Up(migrationBuilder: MigrationBuilder)
      +Down(migrationBuilder: MigrationBuilder)
    }

πŸ†™ Up Method

  • Purpose: Adds the new column to the table.
  • Key Call:
    migrationBuilder.AddColumn<string>(
        name: "WorkOrderType",
        schema: "irby",
        table: "MechanicTimesheet",
        type: "nvarchar(max)",
        nullable: true);
    
  • Effects:
    • Column Name: WorkOrderType
    • Data Type: nvarchar(max)
    • Nullable: true

⬇️ Down Method

  • Purpose: Safely rolls back the schema change by dropping the column.
  • Key Call:
    migrationBuilder.DropColumn(
        name: "WorkOrderType",
        schema: "irby",
        table: "MechanicTimesheet");
    

🧩 Data Model Changes

To fully leverage the new column, update your MechanicTimesheet entity:

namespace IrbyImaging.DbContext.Models
{
    public class MechanicTimesheet
    {
        public int Id { get; set; }
        public int MechanicId { get; set; }
        public DateTime Date { get; set; }
        public decimal HoursWorked { get; set; }

        // πŸ†• New property
        public string? WorkOrderType { get; set; }
    }
}

Updated Table Schema (irby.MechanicTimesheet):

Column Name Data Type Nullable Description
Id int No Primary key
MechanicId int No Foreign key to Mechanic
Date datetime2 No Date of entry
HoursWorked decimal(5,2) No Hours logged
WorkOrderType nvarchar(max) Yes Category of work order

πŸš€ Applying the Migration

  1. Add & Build
    dotnet build
    
  2. Update Database
    dotnet ef database update
    
  3. Verify
    • Inspect the MechanicTimesheet table in SQL Server (e.g., via SSMS)
    • Confirm presence of the WorkOrderType column

πŸ”„ Rolling Back the Migration

To revert:

dotnet ef database update 20240601120000_PreviousMigrationName

Replace 20240601120000_PreviousMigrationName with the target earlier migration. This executes the Down method.


πŸ”— Integration with DbContext

Your DbContext remains unchanged, but ensure your DbSet<MechanicTimesheet> is present:

public class IrbyDbContext : DbContext
{
    public DbSet<MechanicTimesheet> MechanicTimesheet { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultSchema("irby");
        // Other model configurations...
    }
}

⚠️ Error Handling & Considerations

  • Nullability: Because WorkOrderType is nullable, existing rows won’t break.
  • Large Text: nvarchar(max) can store very long stringsβ€”ensure UI/validation handles it.
  • Schema Sync: Always check in your migration file to source control to prevent drift in collaborative teams.

πŸŽ‰ Conclusion

The AddWorkOrderType migration is a simple yet impactful update that enables richer data capture on mechanic timesheets by categorizing work orders. By following the steps above, you can seamlessly apply, use, and (if necessary) roll back this change in your database environment.

Happy coding! πŸš€