π¦ AddCostCodeColumnsToClassificationTable Migration Documentation
π Table of Contents
- Overview & Purpose π
- Dependencies & Technologies π οΈ
- Key Functional Areas π
- Data Model Changes ποΈ
- Migration Methods π
- Usage Examples π
- UML Diagram π
- Rollback & Best Practices π‘οΈ
- Conclusion π
1. Overview & Purpose π
File:
IrbyImaging.DbContext/Migrations/20250117204950_AddCostCodeColumnsToClassificationTable.cs
Purpose:
This Entity Framework Core migration augments the existing JobClassification table (in the "irby" schema) by adding two new columns:
CostCodeName(string): A textual descriptor for cost codes.CostCodeNumber(int): A numeric identifier for cost codes, with a default value of0.
By introducing these fields, the system can now store and query cost-coding information directly alongside classification records.
2. Dependencies & Technologies π οΈ
| Technology / Package | Version / Notes |
|---|---|
| .NET & C# | .NET 6+ / C# 10+ |
| EF Core (Microsoft.EntityFrameworkCore) | 6.x+ / Migrations enabled |
| SQL Server | Target database |
| Project Schema | irby |
Note: Ensure your
DbContextis configured to use the"irby"schema for theJobClassificationentity.
3. Key Functional Areas π
- Schema Evolution: Defines structural changes to the database without manual SQL.
- Up & Down Methods: Encapsulates forward (apply) and backward (rollback) logic.
- Migration Scaffolding: Enables version-controlled, incremental updates.
4. Data Model Changes ποΈ
Below is an overview of the changes to the JobClassification table:
| Column Name | Data Type | Nullable | Default Value | Description |
|---|---|---|---|---|
| CostCodeName | nvarchar(max) |
Yes | N/A | Text label for the cost code. |
| CostCodeNumber | int |
No | 0 |
Numeric identifier for the cost code. |
5. Migration Methods π
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace IrbyImaging.DbContext.Migrations
{
/// <inheritdoc />
public partial class AddCostCodeColumnsToClassificationTable : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "CostCodeName",
schema: "irby",
table: "JobClassification",
type: "nvarchar(max)",
nullable: true);
migrationBuilder.AddColumn<int>(
name: "CostCodeNumber",
schema: "irby",
table: "JobClassification",
type: "int",
nullable: false,
defaultValue: 0);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "CostCodeName",
schema: "irby",
table: "JobClassification");
migrationBuilder.DropColumn(
name: "CostCodeNumber",
schema: "irby",
table: "JobClassification");
}
}
}
Up Method
- Adds
CostCodeNameas a nullablenvarchar(max). - Adds
CostCodeNumberas non-nullableintwith default0.
Down Method
- Removes both
CostCodeNameandCostCodeNumberfrom theJobClassificationtable, reversing theUpoperations.
6. Usage Examples π
6.1. Adding the Migration
# From the project root:
dotnet ef migrations add AddCostCodeColumnsToClassificationTable \
--project IrbyImaging.DbContext \
--startup-project IrbyImaging.WebApi
6.2. Applying the Migration
# Apply to your development database:
dotnet ef database update AddCostCodeColumnsToClassificationTable
6.3. Rolling Back
# Roll back the last migration:
dotnet ef database update <PreviousMigrationName>
7. UML Diagram π
@startuml
package "irby.JobClassification" {
class JobClassification {
+ int Id
+ string Name
+ string CostCodeName <<new>>
+ int CostCodeNumber <<new>>
+ DateTime CreatedDate
+ DateTime ModifiedDate
}
}
package "EFCore.Migrations" {
class AddCostCodeColumnsToClassificationTable {
+ void Up(MigrationBuilder)
+ void Down(MigrationBuilder)
}
}
AddCostCodeColumnsToClassificationTable ..> JobClassification : alters
@enduml
Copy the above PlantUML snippet into a PlantUML-enabled editor to visualize the changes.
8. Rollback & Best Practices π‘οΈ
- Test Migrations Locally before applying to shared or production databases.
- Backup your database or use a migration branch strategy.
- Review SQL generated by the migration:
dotnet ef migrations script AddCostCodeColumnsToClassificationTable - Consistent Naming: Keep migrations self-describing.
9. Conclusion π
This migration cleanly extends the JobClassification table to support cost-coding features. By following the Up/Down pattern, EF Core ensures safe, versioned updatesβallowing teams to evolve schemas collaboratively and rollback when needed.
Embrace these best practices to keep your database schema in sync with your applicationβs domain model!