🗂️ AddCostCodeType Migration Documentation


📑 Table of Contents

  1. Overview & Purpose
  2. Migration Structure
    1. Class Definition
    2. Up Method
    3. Down Method
  3. Dependencies & Technologies
  4. Key Functional Areas
  5. Schema Changes Details
  6. Usage & Execution
  7. Illustrations
  8. Examples
  9. Conclusion

🔍 Overview & Purpose

File: IrbyImaging.DbContext/Migrations/20250206152456_AddCostCodeType.cs
Namespace: IrbyImaging.DbContext.Migrations
Migration Name: AddCostCodeType

This Entity Framework Core migration introduces a new integer column, CostCodeType, into the irby.CostCode table. This enhancement allows the application to differentiate various cost code categories (e.g., labor, materials, overhead) using an enum or numeric identifiers. Without this migration, all cost codes would share the same table schema, making type-specific queries or business logic difficult.


📐 Migration Structure

using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace IrbyImaging.DbContext.Migrations
{
    public partial class AddCostCodeType : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<int>(
                name: "CostCodeType",
                schema: "irby",
                table: "CostCode",
                type: "int",
                nullable: false,
                defaultValue: 0);
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "CostCodeType",
                schema: "irby",
                table: "CostCode");
        }
    }
}

1. Class Definition

  • public partial class AddCostCodeType : Migration
    Defines a partial migration class inheriting from Migration. EF Core uses this to apply or rollback schema changes.

2. Up Method

  • Signature:
    protected override void Up(MigrationBuilder migrationBuilder)
    
  • Purpose:
    Applies the schema change: add the CostCodeType column to irby.CostCode.
  • Key Call:
    migrationBuilder.AddColumn<int>(
        name: "CostCodeType",
        schema: "irby",
        table: "CostCode",
        type: "int",
        nullable: false,
        defaultValue: 0);
    

3. Down Method

  • Signature:
    protected override void Down(MigrationBuilder migrationBuilder)
    
  • Purpose:
    Rolls back the schema change: drop the CostCodeType column if the migration is undone.
  • Key Call:
    migrationBuilder.DropColumn(
        name: "CostCodeType",
        schema: "irby",
        table: "CostCode");
    

⚙️ Dependencies & Technologies

Technology Version / Package Role
Microsoft.EntityFrameworkCore >= 6.0.0 ORM for .NET Core and migrations engine
.NET SDK >= 6.0 Runtime & CLI for EF Core operations
SQL Server (or another RDBMS) — Target database for schema changes

Note: Ensure the Microsoft.EntityFrameworkCore.Design package is installed to scaffold and run migrations.


🔑 Key Functional Areas

  1. Schema Evolution
    • Adding new columns without dropping existing data.
  2. Data Integrity
    • A non-nullable column with default value 0 guarantees backward compatibility.
  3. Rollback Capability
    • The Down method facilitates safe undo of changes.

🏗️ Schema Changes Details

CostCodeType Column

Attribute Description
Name CostCodeType
Data Type int
Schema irby
Table CostCode
Nullable false
Default Value 0
Purpose Classify cost codes by type (e.g., 0 = Undefined, 1 = Labor, etc.)

🚀 Usage & Execution

1. Add the Migration

dotnet ef migrations add AddCostCodeType \
  --project IrbyImaging.DbContext \
  --startup-project IrbyImaging.WebApp

2. Apply to Database

dotnet ef database update AddCostCodeType \
  --project IrbyImaging.DbContext \
  --startup-project IrbyImaging.WebApp

Tip: Omit the migration name in update to apply all pending migrations.

3. Inspect Generated SQL

dotnet ef migrations script \
  --idempotent \
  --output AddCostCodeType.sql

📊 Illustrations

UML Sequence Diagram

sequenceDiagram
    participant Dev as Developer
    participant CLI as EF CLI
    participant Db   as Database
    Dev->>CLI: dotnet ef migrations add AddCostCodeType
    CLI->>Dev: Scaffolds Migration Class
    Dev->>CLI: dotnet ef database update
    CLI->>Db: ALTER TABLE irby.CostCode ADD CostCodeType INT NOT NULL DEFAULT 0
    Db-->>CLI: Success
    CLI-->>Dev: Migration Applied

🔧 Examples

C# Enum Representation

public enum CostCodeType
{
    Undefined = 0,
    Labor     = 1,
    Material  = 2,
    Overhead  = 3
}

public class CostCode
{
    public int Id { get; set; }
    public string Code { get; set; }
    public CostCodeType CostCodeType { get; set; }  // New property
}

Querying by Type

// Retrieve all labor cost codes
var laborCodes = dbContext.CostCodes
    .Where(cc => cc.CostCodeType == CostCodeType.Labor)
    .ToList();

🏁 Conclusion

This migration AddCostCodeType extends the CostCode table by adding a dedicated CostCodeType column, enabling richer classification and querying capabilities. The approach follows EF Core best practices:

  • Safe defaults for backward compatibility
  • Full rollback support via the Down method
  • Clear migration naming for maintainability

By running the provided EF Core CLI commands, you can effortlessly integrate this schema evolution into your database management workflow and leverage type-safe cost code operations in your application. 🎉


Happy coding! 🚀