πŸ“„ DeleteNewColumnDocumentImageTable Migration Documentation

File: IrbyImaging.DbContext/Migrations/20221103025954_DeleteNewColumnDocumentImageTable.cs
Namespace: IrbyImaging.DbContext.Migrations
EF Core Version: 6.0+


Table of Contents

  1. Overview & Purpose πŸ“–
  2. Key Functional Areas πŸ› οΈ
  3. Dependencies & Technologies 🧩
  4. Data Model Changes πŸ—„οΈ
  5. Usage & Examples πŸ“š
  6. UML Diagram πŸ“ˆ
  7. 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)
  • 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 CustomerName and CustomerNumber
    • Targets table: irby.DocumentImage
  • 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 Up changes

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
  • --project points to your DbContext project.
  • --startup-project points 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! ✨