🗂️ AddCostCodeType Migration Documentation
📑 Table of Contents
- Overview & Purpose
- Migration Structure
- Dependencies & Technologies
- Key Functional Areas
- Schema Changes Details
- Usage & Execution
- Illustrations
- Examples
- 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 fromMigration. 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 theCostCodeTypecolumn toirby.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 theCostCodeTypecolumn 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.Designpackage is installed to scaffold and run migrations.
🔑 Key Functional Areas
- Schema Evolution
- Adding new columns without dropping existing data.
- Data Integrity
- A non-nullable column with default value
0guarantees backward compatibility.
- A non-nullable column with default value
- Rollback Capability
- The
Downmethod facilitates safe undo of changes.
- The
🏗️ 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
updateto 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
Downmethod - 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! 🚀