📑 Documentation: 20240613160449_ChangePncIntFieldsToString2 Migration


📚 Table of Contents

  1. Overview & Purpose
  2. Key Functional Areas
  3. Dependencies & Technologies
  4. Migration Class Structure
  5. Data Model Impact
  6. How to Apply / Revert This Migration
  7. Sample Code Snippets
  8. UML Diagram
  9. Conclusion

📝 Overview & Purpose

This Entity Framework Core migration alters the data type of the MCC column in the irby.Pnc table from an integer (int) to a string (nvarchar(max)).

  • Why?
    • To accommodate non-numeric values (e.g., leading zeros, alphanumeric codes).
    • Prevent data loss or conversion errors when entries exceed integer bounds or contain special characters.

⚙️ Key Functional Areas

Area Description
Column Alteration Change MCC from int → string (nullable) in the Up phase.
Rollback Capability Revert MCC from string → int (non-nullable, default 0) in the Down phase.
Schema Specification The table resides in the irby schema.
EF Core Migration Built upon Microsoft.EntityFrameworkCore.Migrations.Migration.

🛠️ Dependencies & Technologies

  • .NET / .NET Core (6.0+ or compatible)
  • EntityFrameworkCore
    • Package: Microsoft.EntityFrameworkCore
    • Package: Microsoft.EntityFrameworkCore.Design
  • Database Server
    • Microsoft SQL Server (or any provider supporting nvarchar(max))

🔍 Migration Class Structure

namespace IrbyImaging.DbContext.Migrations
{
    public partial class ChangePncIntFieldsToString2 : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder) { /* ... */ }

        protected override void Down(MigrationBuilder migrationBuilder) { /* ... */ }
    }
}

Up Method

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.AlterColumn<string>(
        name: "MCC",
        schema: "irby",
        table: "Pnc",
        type: "nvarchar(max)",
        nullable: true,
        oldClrType: typeof(int),
        oldType: "int");
}
  • Action: Alters irby.Pnc.MCC from int → nvarchar(max).
  • Nullability: Sets nullable: true, allowing NULL values.
  • Old Type Metadata:
    • oldClrType: typeof(int)
    • oldType: "int"

Down Method

protected override void Down(MigrationBuilder migrationBuilder)
{
    migrationBuilder.AlterColumn<int>(
        name: "MCC",
        schema: "irby",
        table: "Pnc",
        type: "int",
        nullable: false,
        defaultValue: 0,
        oldClrType: typeof(string),
        oldType: "nvarchar(max)",
        oldNullable: true);
}
  • Action: Reverts MCC back to int.
  • Nullability: Enforces nullable: false with a defaultValue: 0.
  • Old Type Metadata:
    • oldClrType: typeof(string)
    • oldType: "nvarchar(max)"
    • oldNullable: true

🗃️ Data Model Impact

👉 Before Migration

public class Pnc
{
    public int MCC { get; set; }
    // ... other properties
}

👉 After Migration

public class Pnc
{
    public string? MCC { get; set; }
    // ... other properties
}

Tip: Update any data annotations or Fluent API configurations to reflect the new type.


🚀 How to Apply / Revert This Migration

  1. Add Migration (if not yet added):
    dotnet ef migrations add ChangePncIntFieldsToString2
    
  2. Apply Migration:
    dotnet ef database update
    
  3. Revert (Rollback):
    dotnet ef database update LastGoodMigrationName
    

💡 Sample Code Snippets

Adding a New Migration

dotnet ef migrations add ChangePncIntFieldsToString2 \
    --context IrbyDbContext \
    --output-dir Migrations

Updating the Database

dotnet ef database update

Checking Migration Status

dotnet ef migrations list

📊 UML Diagram

classDiagram
    class ChangePncIntFieldsToString2 {
        +Up(MigrationBuilder)
        +Down(MigrationBuilder)
    }
    ChangePncIntFieldsToString2 ..|> Migration

Note: This simple class diagram shows the inheritance from Migration and the two primary methods.


🎯 Conclusion

This migration ensures that your Pnc.MCC column can store alphanumeric or variable-length codes by switching from int to string.

  • Forward: Safely accommodates new data formats.
  • Backward: Provides a clean rollback path with default values.

By following the steps above, you can seamlessly evolve your schema without disrupting existing data or application logic.

Happy migrating! 🚀✨