π Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Migration Details
- Applying & Rolling Back Migrations π
- Code Snippets & Examples π»
- UML Diagram π
- Conclusion π―
1. Overview & Purpose
File path:
IrbyImaging.DbContext/Migrations/20250113133014_AddTableInfoForCostCode.cs
What it is:
This C# class is an Entity Framework Core Migration that reorganizes the CostCodes table:
- Renames the table from
CostCodes(default schema) toirby.CostCode. - Updates the primary key constraint name to reflect the new table name and schema.
Why it matters:
- Enforces a consistent naming convention (
singulartable names under theirbyschema). - Improves database organization by grouping domain tables under a specific schema.
2. Dependencies & Technologies
| Component | Version / Details |
|---|---|
| .NET / C# | .NET 6.0+ / C# 10+ |
| Entity Framework Core | EF Core migrations (Microsoft.EntityFrameworkCore.Migrations) |
| Database | SQL Server (or any EF Coreβcompatible store) |
| Tooling | dotnet ef CLI, Visual Studio / Rider |
3. Migration Details
3.1 Migration Class Structure
namespace IrbyImaging.DbContext.Migrations
{
public partial class AddTableInfoForCostCode : Migration
{
protected override void Up(MigrationBuilder migrationBuilder) { /* ... */ }
protected override void Down(MigrationBuilder migrationBuilder) { /* ... */ }
}
}
- Inheritance: Inherits from
Migration(EF Core base class). - Name pattern:
YYYYMMDDHHMMSS_DescriptiveName. - Two core methods:
Up(...)applies the schema changes.Down(...)reverts them.
3.2 Up() Method πΌ
The Up method applies the following operations:
| Step | Operation | Parameters |
|---|---|---|
| 1 | DropPrimaryKey | name: "PK_CostCodes", table: "CostCodes" |
| 2 | RenameTable | name: "CostCodes", newName: "CostCode", newSchema: "irby" |
| 3 | AddPrimaryKey | name: "PK_CostCode", schema: "irby", table: "CostCode", column: "CostCodeId" |
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropPrimaryKey(
name: "PK_CostCodes",
table: "CostCodes");
migrationBuilder.RenameTable(
name: "CostCodes",
newName: "CostCode",
newSchema: "irby");
migrationBuilder.AddPrimaryKey(
name: "PK_CostCode",
schema: "irby",
table: "CostCode",
column: "CostCodeId");
}
π Key Points:
- DropPrimaryKey must precede renaming.
- RenameTable moves it under the
irbyschema and changes the pluralization. - AddPrimaryKey re-creates the constraint with the updated name & schema.
3.3 Down() Method π½
The Down method reverses Up:
| Step | Operation | Parameters |
|---|---|---|
| 1 | DropPrimaryKey | name: "PK_CostCode", schema: "irby", table: "CostCode" |
| 2 | RenameTable | name: "CostCode", schema: "irby", newName: "CostCodes" |
| 3 | AddPrimaryKey | name: "PK_CostCodes", table: "CostCodes", column: "CostCodeId" |
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropPrimaryKey(
name: "PK_CostCode",
schema: "irby",
table: "CostCode");
migrationBuilder.RenameTable(
name: "CostCode",
schema: "irby",
newName: "CostCodes");
migrationBuilder.AddPrimaryKey(
name: "PK_CostCodes",
table: "CostCodes",
column: "CostCodeId");
}
π Rollback Logic: Ensures safe downgrade to the previous table name and default schema.
4. Applying & Rolling Back Migrations π
-
Add Migration (already done):
dotnet ef migrations add AddTableInfoForCostCode -
Apply to Database:
dotnet ef database update -
Verify Schema Change:
- Check that
irby.CostCodeexists. - Confirm
PK_CostCodeis the primary key.
- Check that
-
Rollback to Previous Migration:
dotnet ef database update <Previous_Migration_Name>
5. Code Snippets & Examples π»
-- After Up()
SELECT *
FROM irby.CostCode; -- β
New table path
EXEC sp_helpconstraint
'irby.CostCode', 'nomsg';
-- Shows PK_CostCode on CostCodeId
-- After Down()
SELECT *
FROM dbo.CostCodes; -- β
Reverted to old name
EXEC sp_helpconstraint
'dbo.CostCodes', 'nomsg';
-- Shows PK_CostCodes on CostCodeId
6. UML Diagram π
+-------------------------------------+
| Migration: AddTableInfoForCostCode |
+-------------------------------------+
| +void Up(MigrationBuilder) |
| +void Down(MigrationBuilder) |
+-------------------------------------+
^ ^
| |
inherits Migration |
(EF Core Base Class) |
|
+--------------------------------------+------+
| MigrationBuilder |
+---------------------------------------------+
| +DropPrimaryKey(name, table, schema?) |
| +RenameTable(name, newName, schema?) |
| +AddPrimaryKey(name, table, column, schema?) |
+---------------------------------------------+
7. Conclusion π―
This migration centralizes the CostCode table under the irby schema and unifies naming to a singular form. Such changes improve clarity, reduce ambiguity, and align with best practices in database design.
With this documentation, you have:
- A clear overview of its purpose
- Step-by-step breakdown of
Up/Downmethods - Practical examples for applying and verifying the change
Feel free to integrate, test, and extend this pattern for other tables in your domain!