πŸ“¦ AddCostCodeColumnsToClassificationTable Migration Documentation


πŸ“‘ Table of Contents

  1. Overview & Purpose πŸ“–
  2. Dependencies & Technologies πŸ› οΈ
  3. Key Functional Areas πŸ”‘
  4. Data Model Changes πŸ—ƒοΈ
  5. Migration Methods πŸ”„
  6. Usage Examples πŸš€
  7. UML Diagram πŸ“Š
  8. Rollback & Best Practices πŸ›‘οΈ
  9. Conclusion πŸŽ‰

1. Overview & Purpose πŸ“–

File:
IrbyImaging.DbContext/Migrations/20250117204950_AddCostCodeColumnsToClassificationTable.cs

Purpose:
This Entity Framework Core migration augments the existing JobClassification table (in the "irby" schema) by adding two new columns:

  • CostCodeName (string): A textual descriptor for cost codes.
  • CostCodeNumber (int): A numeric identifier for cost codes, with a default value of 0.

By introducing these fields, the system can now store and query cost-coding information directly alongside classification records.


2. Dependencies & Technologies πŸ› οΈ

Technology / Package Version / Notes
.NET & C# .NET 6+ / C# 10+
EF Core (Microsoft.EntityFrameworkCore) 6.x+ / Migrations enabled
SQL Server Target database
Project Schema irby

Note: Ensure your DbContext is configured to use the "irby" schema for the JobClassification entity.


3. Key Functional Areas πŸ”‘

  • Schema Evolution: Defines structural changes to the database without manual SQL.
  • Up & Down Methods: Encapsulates forward (apply) and backward (rollback) logic.
  • Migration Scaffolding: Enables version-controlled, incremental updates.

4. Data Model Changes πŸ—ƒοΈ

Below is an overview of the changes to the JobClassification table:

Column Name Data Type Nullable Default Value Description
CostCodeName nvarchar(max) Yes N/A Text label for the cost code.
CostCodeNumber int No 0 Numeric identifier for the cost code.

5. Migration Methods πŸ”„

using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

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

            migrationBuilder.AddColumn<int>(
                name: "CostCodeNumber",
                schema: "irby",
                table: "JobClassification",
                type: "int",
                nullable: false,
                defaultValue: 0);
        }

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

            migrationBuilder.DropColumn(
                name: "CostCodeNumber",
                schema: "irby",
                table: "JobClassification");
        }
    }
}

Up Method

  • Adds CostCodeName as a nullable nvarchar(max).
  • Adds CostCodeNumber as non-nullable int with default 0.

Down Method

  • Removes both CostCodeName and CostCodeNumber from the JobClassification table, reversing the Up operations.

6. Usage Examples πŸš€

6.1. Adding the Migration

# From the project root:
dotnet ef migrations add AddCostCodeColumnsToClassificationTable \
    --project IrbyImaging.DbContext \
    --startup-project IrbyImaging.WebApi

6.2. Applying the Migration

# Apply to your development database:
dotnet ef database update AddCostCodeColumnsToClassificationTable

6.3. Rolling Back

# Roll back the last migration:
dotnet ef database update <PreviousMigrationName>

7. UML Diagram πŸ“Š

@startuml
package "irby.JobClassification" {
  class JobClassification {
    + int Id
    + string Name
    + string CostCodeName    <<new>>
    + int CostCodeNumber     <<new>>
    + DateTime CreatedDate
    + DateTime ModifiedDate
  }
}

package "EFCore.Migrations" {
  class AddCostCodeColumnsToClassificationTable {
    + void Up(MigrationBuilder)
    + void Down(MigrationBuilder)
  }
}
AddCostCodeColumnsToClassificationTable ..> JobClassification : alters
@enduml

Copy the above PlantUML snippet into a PlantUML-enabled editor to visualize the changes.


8. Rollback & Best Practices πŸ›‘οΈ

  • Test Migrations Locally before applying to shared or production databases.
  • Backup your database or use a migration branch strategy.
  • Review SQL generated by the migration:
    dotnet ef migrations script AddCostCodeColumnsToClassificationTable
    
  • Consistent Naming: Keep migrations self-describing.

9. Conclusion πŸŽ‰

This migration cleanly extends the JobClassification table to support cost-coding features. By following the Up/Down pattern, EF Core ensures safe, versioned updatesβ€”allowing teams to evolve schemas collaboratively and rollback when needed.

Embrace these best practices to keep your database schema in sync with your application’s domain model!