πŸ“‘ Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Migration Details
  4. Applying & Rolling Back Migrations πŸš€
  5. Code Snippets & Examples πŸ’»
  6. UML Diagram πŸ“ˆ
  7. Conclusion 🎯

1. Overview & Purpose

File path:

IrbyImaging.DbContext/Migrations/20250113133014_AddTableInfoForCostCode.cs

What it is:
This C# class is an Entity Framework Core Migration that reorganizes the CostCodes table:

  • Renames the table from CostCodes (default schema) to irby.CostCode.
  • Updates the primary key constraint name to reflect the new table name and schema.

Why it matters:

  • Enforces a consistent naming convention (singular table names under the irby schema).
  • Improves database organization by grouping domain tables under a specific schema.

2. Dependencies & Technologies

Component Version / Details
.NET / C# .NET 6.0+ / C# 10+
Entity Framework Core EF Core migrations (Microsoft.EntityFrameworkCore.Migrations)
Database SQL Server (or any EF Core–compatible store)
Tooling dotnet ef CLI, Visual Studio / Rider

3. Migration Details

3.1 Migration Class Structure

namespace IrbyImaging.DbContext.Migrations
{
    public partial class AddTableInfoForCostCode : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder) { /* ... */ }
        protected override void Down(MigrationBuilder migrationBuilder) { /* ... */ }
    }
}
  • Inheritance: Inherits from Migration (EF Core base class).
  • Name pattern: YYYYMMDDHHMMSS_DescriptiveName.
  • Two core methods:
    • Up(...) applies the schema changes.
    • Down(...) reverts them.

3.2 Up() Method πŸ”Ό

The Up method applies the following operations:

Step Operation Parameters
1 DropPrimaryKey name: "PK_CostCodes", table: "CostCodes"
2 RenameTable name: "CostCodes", newName: "CostCode", newSchema: "irby"
3 AddPrimaryKey name: "PK_CostCode", schema: "irby", table: "CostCode", column: "CostCodeId"
protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.DropPrimaryKey(
        name: "PK_CostCodes",
        table: "CostCodes");

    migrationBuilder.RenameTable(
        name: "CostCodes",
        newName: "CostCode",
        newSchema: "irby");

    migrationBuilder.AddPrimaryKey(
        name: "PK_CostCode",
        schema: "irby",
        table: "CostCode",
        column: "CostCodeId");
}

πŸ”‘ Key Points:

  • DropPrimaryKey must precede renaming.
  • RenameTable moves it under the irby schema and changes the pluralization.
  • AddPrimaryKey re-creates the constraint with the updated name & schema.

3.3 Down() Method πŸ”½

The Down method reverses Up:

Step Operation Parameters
1 DropPrimaryKey name: "PK_CostCode", schema: "irby", table: "CostCode"
2 RenameTable name: "CostCode", schema: "irby", newName: "CostCodes"
3 AddPrimaryKey name: "PK_CostCodes", table: "CostCodes", column: "CostCodeId"
protected override void Down(MigrationBuilder migrationBuilder)
{
    migrationBuilder.DropPrimaryKey(
        name: "PK_CostCode",
        schema: "irby",
        table: "CostCode");

    migrationBuilder.RenameTable(
        name: "CostCode",
        schema: "irby",
        newName: "CostCodes");

    migrationBuilder.AddPrimaryKey(
        name: "PK_CostCodes",
        table: "CostCodes",
        column: "CostCodeId");
}

πŸ”„ Rollback Logic: Ensures safe downgrade to the previous table name and default schema.


4. Applying & Rolling Back Migrations πŸš€

  1. Add Migration (already done):

    dotnet ef migrations add AddTableInfoForCostCode
    
  2. Apply to Database:

    dotnet ef database update
    
  3. Verify Schema Change:

    • Check that irby.CostCode exists.
    • Confirm PK_CostCode is the primary key.
  4. Rollback to Previous Migration:

    dotnet ef database update <Previous_Migration_Name>
    

5. Code Snippets & Examples πŸ’»

-- After Up()
SELECT * 
FROM irby.CostCode;        -- βœ… New table path

EXEC sp_helpconstraint 
    'irby.CostCode', 'nomsg';  
-- Shows PK_CostCode on CostCodeId
-- After Down()
SELECT * 
FROM dbo.CostCodes;        -- βœ… Reverted to old name

EXEC sp_helpconstraint 
    'dbo.CostCodes', 'nomsg';  
-- Shows PK_CostCodes on CostCodeId

6. UML Diagram πŸ“ˆ

+-------------------------------------+
|         Migration: AddTableInfoForCostCode         |
+-------------------------------------+
| +void Up(MigrationBuilder)         |
| +void Down(MigrationBuilder)       |
+-------------------------------------+
          ^                ^
          |                |
  inherits Migration       |
  (EF Core Base Class)     |
                           |
   +--------------------------------------+------+  
   |            MigrationBuilder                   |
   +---------------------------------------------+
   | +DropPrimaryKey(name, table, schema?)        |
   | +RenameTable(name, newName, schema?)         |
   | +AddPrimaryKey(name, table, column, schema?) |
   +---------------------------------------------+

7. Conclusion 🎯

This migration centralizes the CostCode table under the irby schema and unifies naming to a singular form. Such changes improve clarity, reduce ambiguity, and align with best practices in database design.

With this documentation, you have:

  • A clear overview of its purpose
  • Step-by-step breakdown of Up/Down methods
  • Practical examples for applying and verifying the change

Feel free to integrate, test, and extend this pattern for other tables in your domain!