π¦ Documentation: RemoveZipCodeColumn Migration
File Path:
IrbyImaging.DbContext/Migrations/20250116130013_RemoveZipCodeColumn.cs
ποΈ Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Migration Class Structure
- Key Functional Areas
- 4.1 Up Method
- 4.2 Down Method
- Data Model Context
- Usage & Examples
- Integration into CI/CD & Version Control
- UML Diagram
- Conclusion
π§ Overview & Purpose
This Entity Framework Core migration removes the ZipCode column from the County table in the irby schema.
- Why? Perhaps the application no longer needs to store ZIP codes at the county level, or it has been consolidated into another entity.
- Who? Generated as part of a schema refactor by a database developer or via the
dotnet ef migrationsCLI. - When? Timestamped in the filename:
2025-01-16T13:00:13Z.
π§ Dependencies & Technologies
| Technology / Package | Version | Purpose |
|---|---|---|
| Microsoft.EntityFrameworkCore | β₯ 6.0 | Core ORM |
| Microsoft.EntityFrameworkCore.Relational | β₯ 6.0 | Relational database migrations |
| .NET SDK | 6.0+ | Runtime & CLI tooling |
Note: Ensure your
.csprojincludes:<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.*" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.*" />
ποΈ Migration Class Structure
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace IrbyImaging.DbContext.Migrations
{
/// <inheritdoc />
public partial class RemoveZipCodeColumn : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "ZipCode",
schema: "irby",
table: "County");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "ZipCode",
schema: "irby",
table: "County",
type: "nvarchar(max)",
nullable: true);
}
}
}
- Namespace:
IrbyImaging.DbContext.Migrations - Class:
RemoveZipCodeColumninherits fromMigration - Both methods override the EF Core migration contract.
π Key Functional Areas
| Method | Operation | Description |
|---|---|---|
| Up | DropColumn("ZipCode") |
Removes the ZipCode column from the County table in the irby schema. |
| Down | AddColumn<string>("ZipCode") |
Re-adds the ZipCode column as nvarchar(max) (nullable) if rolling back this migration. |
4.1 Up Method
- Signature:
protected override void Up(MigrationBuilder migrationBuilder) - Behavior:
- Calls
migrationBuilder.DropColumn(...). - Ensures the column is removed at the database level on
database update.
- Calls
4.2 Down Method
- Signature:
protected override void Down(MigrationBuilder migrationBuilder) - Behavior:
- Reverts the change by adding back the column with the same name, type, and nullability.
ποΈ Data Model Context
Entity Affected:
County
Although the code file for County is not shown here, you can expect something like:
namespace IrbyImaging.Domain.Entities
{
public class County
{
public int Id { get; set; }
public string Name { get; set; }
// public string ZipCode { get; set; } <- Removed by this migration
// ... other properties ...
}
}
- After applying
RemoveZipCodeColumn, theCountyclass should no longer define aZipCodeproperty to stay in sync with the database schema.
π οΈ Usage & Examples
-
Add the Migration
If you havenβt already generated it:dotnet ef migrations add RemoveZipCodeColumn --schema irby -
Apply to Database
dotnet ef database update -
Rollback (if needed)
dotnet ef database update LastGoodMigrationName
π Integration into CI/CD & Version Control
-
Version Control:
- Commit this file alongside your code changes.
- Use a clear commit message:
feat(db): remove ZipCode column from County table
-
Automated Deployments:
- In your CI pipeline, run:
dotnet ef database update --no-build - Ensures the DB schema is in sync before running integration tests or starting the application.
- In your CI pipeline, run:
π UML Diagram
classDiagram
class Migration {
<<abstract>>
+Up(MigrationBuilder)
+Down(MigrationBuilder)
}
class RemoveZipCodeColumn {
+Up(MigrationBuilder)
+Down(MigrationBuilder)
}
Migration <|-- RemoveZipCodeColumn
class MigrationBuilder {
+DropColumn(name, schema?, table)
+AddColumn<T>(name, schema?, table, type, nullable)
}
RemoveZipCodeColumn ..> MigrationBuilder : uses
β Conclusion
This migration is a simple yet pivotal step in maintaining a clean, purpose-driven database schema. By removing unused columns:
- β Reduces storage bloat.
- β Simplifies your domain model.
- β Avoids confusion in future feature development.
Next Steps:
- Remove any references to
ZipCodein code, tests, or front-end views. - Ensure all teammates are aware of the schema change.
- Run end-to-end tests to verify no regressions.
π Happy Coding! π
If you have further questions, feel free to reach out to the database team or consult the EF Core docs:
https://docs.microsoft.com/ef/core/managing-schemas/migrations/