π¦ Migration: CostCodeNumberChange (20250116161541)
IrbyImaging.DbContext/
ββ Migrations/
ββ 20250116161541_CostCodeNumberChange.cs
π Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Data Model:
CostCodeEntity - Migration Class Breakdown
- 4.1. Class Definition
- 4.2. Up Method
- 4.3. Down Method
- UML Diagram
- Usage & Execution
- 6.1. Adding the Migration
- 6.2. Applying the Migration
- 6.3. Reverting the Migration
- Error Handling & Rollback Strategy
- Best Practices & Considerations
- Conclusion
π§ Overview & Purpose
This Entity Framework Core migration alters the CostCodeNumber column type in the irby.CostCode table:
- Before (
Down):- CLR type:
int - SQL type:
int
- CLR type:
- After (
Up):- CLR type:
string - SQL type:
nvarchar(max)
- CLR type:
Why?
- Some cost codes may include leading zeros or alphanumeric patterns (e.g.,
"A100","007B"). - Switching to
stringavoids data loss or formatting constraints.
π§ Dependencies & Technologies
| Technology/Package | Version (example) | Purpose |
|---|---|---|
| Microsoft.EntityFrameworkCore | β₯ 7.0.0 | Core ORM for migrations & data access |
| Microsoft.EntityFrameworkCore.SqlServer | β₯ 7.0.0 | SQL Server provider |
| .NET 7 (or later) | 7.0+ | Target framework |
Note: Ensure your project references
Microsoft.EntityFrameworkCore.Designif you scaffold or update migrations via CLI.
ποΈ Data Model: CostCode Entity
File:
IrbyImaging.DbContext/Entities/CostCode.cs
(Hypothetical location β illustrates context)
public class CostCode
{
public int Id { get; set; }
// Changed from int to string by this migration
public string? CostCodeNumber { get; set; }
public string Description { get; set; } = string.Empty;
// ... other properties ...
}
DbContextregistration:public class IrbyContext : DbContext { public DbSet<CostCode> CostCodes { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<CostCode>() .ToTable("CostCode", schema: "irby"); } }
π¨ Migration Class Breakdown
4.1 Class Definition
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace IrbyImaging.DbContext.Migrations
{
/// <inheritdoc />
public partial class CostCodeNumberChange : Migration
{
// ...
}
}
- Namespace:
IrbyImaging.DbContext.Migrations - Class:
CostCodeNumberChange - Base:
Migration(from EF Core)
4.2 Up Method π
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<string>(
name: "CostCodeNumber",
schema: "irby",
table: "CostCode",
type: "nvarchar(max)",
nullable: true,
oldClrType: typeof(int),
oldType: "int");
}
What it does
- Alters column
CostCodeNumberin schemairby.CostCode. - Changes from
int NOT NULLβnvarchar(max) NULL.
| Parameter | Description |
|---|---|
name |
Column name ("CostCodeNumber") |
schema |
DB schema ("irby") |
table |
Table name ("CostCode") |
type |
New SQL type ("nvarchar(max)") |
nullable |
Allows NULL values (true) |
oldClrType |
CLR type before change (typeof(int)) |
oldType |
Old SQL type ("int") |
4.3 Down Method βͺ
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<int>(
name: "CostCodeNumber",
schema: "irby",
table: "CostCode",
type: "int",
nullable: false,
defaultValue: 0,
oldClrType: typeof(string),
oldType: "nvarchar(max)",
oldNullable: true);
}
What it does
- Reverts
CostCodeNumberβint NOT NULL, defaulting to0.
| Parameter | Description |
|---|---|
defaultValue |
Sets default for existing NULL rows to 0 |
oldNullable |
Indicates previous nullability (true) |
π UML Diagram
classDiagram
class Migration {
<<interface>>
+ Up(MigrationBuilder)
+ Down(MigrationBuilder)
}
class CostCodeNumberChange {
+ Up(MigrationBuilder)
+ Down(MigrationBuilder)
}
Migration <|-- CostCodeNumberChange
π Usage & Execution
6.1 Adding the Migration
dotnet ef migrations add CostCodeNumberChange \
--project IrbyImaging.DbContext \
--startup-project IrbyImaging.Api
--project: Points to yourDbContextproject--startup-project: Points to the API or host project
6.2 Applying the Migration
dotnet ef database update \
--project IrbyImaging.DbContext \
--startup-project IrbyImaging.Api
- Applies all pending migrations, including
CostCodeNumberChange.
6.3 Reverting the Migration
- To rollback one step:
dotnet ef database update PreviousMigrationName - Or to revert completely:
dotnet ef database update 0
β οΈ Error Handling & Rollback Strategy
- Data truncation risk: Converting from
intβstringis safe, but rolling back may lose alphanumeric codes. - Recommended:
- Back up your database.
- Validate existing data for nonβnumeric values before applying Down.
- Use
defaultValuecarefully to avoid unintended zeros.
π οΈ Best Practices & Considerations
- Nullability: Allowing
NULLprovides flexibilityβbut ensure your application handlesnullor empty strings. - Data Validation: Consider adding a Check Constraint or Regex at the model level to enforce allowed formats.
- Versioning: Always include a meaningful timestamp or name in the migration filename for traceability.
β Conclusion
This migration is a simple yet critical schema change allowing richer cost code formats. It ensures:
- Flexibility for alphanumeric codes
- Minimal performance impact
- A reversible path via the
Downmethod
Happy migrating! π
For further queries, reach out to your teamβs EF Core expert or consult the official Microsoft docs.