📑 Documentation: 20240613160449_ChangePncIntFieldsToString2 Migration
📚 Table of Contents
- Overview & Purpose
- Key Functional Areas
- Dependencies & Technologies
- Migration Class Structure
- Data Model Impact
- How to Apply / Revert This Migration
- Sample Code Snippets
- UML Diagram
- 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
- Package:
- Database Server
- Microsoft SQL Server (or any provider supporting
nvarchar(max))
- Microsoft SQL Server (or any provider supporting
🔍 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.MCCfromint→nvarchar(max). - Nullability: Sets
nullable: true, allowingNULLvalues. - 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
MCCback toint. - Nullability: Enforces
nullable: falsewith adefaultValue: 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
- Add Migration (if not yet added):
dotnet ef migrations add ChangePncIntFieldsToString2 - Apply Migration:
dotnet ef database update - 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
Migrationand 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! 🚀✨