π DeleteNewColumnDocumentImageTable Migration Documentation
File:
IrbyImaging.DbContext/Migrations/20221103025954_DeleteNewColumnDocumentImageTable.cs
Namespace:IrbyImaging.DbContext.Migrations
EF Core Version: 6.0+
Table of Contents
- Overview & Purpose π
- Key Functional Areas π οΈ
- Dependencies & Technologies π§©
- Data Model Changes ποΈ
- Usage & Examples π
- UML Diagram π
- Conclusion π―
Overview & Purpose π
This Entity Framework Core migration removes two previously added columns from the DocumentImage table in the irby schema:
- CustomerName
- CustomerNumber
By applying this migration, any traces of these columns will be dropped from the database schema. Rolling back (via the Down method) will reintroduce them as nvarchar(max) columns, nullable by default.
Why?
- To clean up schema changes that are no longer required.
- To enforce data normalization by moving customer details elsewhere.
- To reduce table bloat and improve performance.
Key Functional Areas π οΈ
| Area | Description |
|---|---|
| Migration Class | DeleteNewColumnDocumentImageTable inherits from Migration. |
| Up Method | Drops the two columns from the irby.DocumentImage table. |
| Down Method | Re-adds the two columns (in case of rollback). |
| Schema Management | Operates on the irby schema. |
| Version Control | Filename prefix (20221103025954_β¦) ensures chronological ordering. |
Dependencies & Technologies π§©
- .NET 6.0+
- EntityFrameworkCore
- Package:
Microsoft.EntityFrameworkCore - Package:
Microsoft.EntityFrameworkCore.Design(for tooling) - Package:
Microsoft.EntityFrameworkCore.SqlServer(assuming SQL Server provider)
- Package:
- C# 10 / .NET CLI
- Migrations: Enables codeβfirst schema evolution
Data Model Changes ποΈ
Up Method
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "CustomerName",
schema: "irby",
table: "DocumentImage");
migrationBuilder.DropColumn(
name: "CustomerNumber",
schema: "irby",
table: "DocumentImage");
}
What it does:
- DropColumn
- Removes
CustomerNameandCustomerNumber - Targets table:
irby.DocumentImage
- Removes
- Ensures irreversible schema change (dangerous if data still exists!)
Down Method
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "CustomerName",
schema: "irby",
table: "DocumentImage",
type: "nvarchar(max)",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "CustomerNumber",
schema: "irby",
table: "DocumentImage",
type: "nvarchar(max)",
nullable: true);
}
What it does:
- AddColumn
- Recreates the two columns
- Type:
nvarchar(max) - Nullable: true
- Safely reverts the
Upchanges
Usage & Examples π
Below are common .NET CLI commands to manage EF Core migrations.
Adding the Migration
dotnet ef migrations add DeleteNewColumnDocumentImageTable \
--project IrbyImaging.DbContext \
--startup-project IrbyImaging.WebApi
--projectpoints to your DbContext project.--startup-projectpoints to the application that will run Migrations (e.g., Web API).
Applying the Migration
dotnet ef database update \
--project IrbyImaging.DbContext \
--startup-project IrbyImaging.WebApi
This will execute the Up method and drop the two columns.
Rolling Back the Migration
dotnet ef database update 20221103000000_OldMigrationName
Replace 20221103000000_OldMigrationName with the previous migration timestamp.
This runs the Down method of DeleteNewColumnDocumentImageTable.
UML Diagram π
Below is a Mermaid class diagram illustrating the migration class relationship:
classDiagram
note for Migration "EF Core Migration Base Class"
Migration <|-- DeleteNewColumnDocumentImageTable : inherits
class DeleteNewColumnDocumentImageTable {
+void Up(MigrationBuilder migrationBuilder)
+void Down(MigrationBuilder migrationBuilder)
}
class MigrationBuilder {
+DropColumn(string name, string schema, string table)
+AddColumn<T>(...)
<<utility>>
}
Conclusion π―
With this migration:
- You cleanly remove obsolete customer fields from
DocumentImage. - The schema remains versioned and can be rolled back if necessary.
- Leveraging EF Core Migrations ensures that your database schema stays in sync with your code model.
β Always back up data before dropping columns!
π Test rollback in a nonβproduction environment.
Happy coding! β¨