📝 Documentation: 20221103025134_DeleteColumnCustomerTable.cs Migration


Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Key Functional Areas
  4. Migration Class Structure
  5. Usage & Execution
  6. UML Class Diagram
  7. Conclusion

1. Overview & Purpose 🌟

This file is an Entity Framework Core migration that drops the CustomerName column from the Customer table in the irby schema.

  • File Path:
    IrbyImaging.DbContext/Migrations/20221103025134_DeleteColumnCustomerTable.cs

  • Intended Use:

    • Modify the database schema by removing an obsolete column.
    • Provide a reversible operation allowing rollback to the previous state.

Why?
Over time, application requirements evolve. When a particular property (here, CustomerName) is no longer needed, removing it keeps the schema clean and efficient.


2. Dependencies & Technologies 🛠️

Technology Version/Reference Purpose
.NET Core / .NET 5+ EF Core 5.0+ ORM for database schema migrations
Microsoft.EntityFrameworkCore.Migrations NuGet Package Provides MigrationBuilder & migration scaffolding
SQL Server (or other RDBMS) Provider-specific Target database for applying schema changes

3. Key Functional Areas 🔑

  1. Migration Class Definition
  2. Up() Method – Defines the schema change to apply.
  3. Down() Method – Defines how to revert the change.

4. Migration Class Structure 🏗️

using Microsoft.EntityFrameworkCore.Migrations;

namespace IrbyImaging.DbContext.Migrations
{
    public partial class DeleteColumnCustomerTable : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "CustomerName",
                schema: "irby",
                table: "Customer");
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<string>(
                name: "CustomerName",
                schema: "irby",
                table: "Customer",
                type: "nvarchar(max)",
                nullable: true);
        }
    }
}

4.1 Up Method 🚀

Purpose: Apply the migration.

  • Operation:
    Drops the CustomerName column from the irby.Customer table.

  • Key Call:
    migrationBuilder.DropColumn(...)

  • Parameters:

    • name: Column name to remove ("CustomerName").
    • schema: Schema of target table ("irby").
    • table: Table name ("Customer").

Code Snippet:

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.DropColumn(
        name: "CustomerName",
        schema: "irby",
        table: "Customer");
}

4.2 Down Method 🔄

Purpose: Revert the migration.

  • Operation:
    Adds back the CustomerName column with type nvarchar(max) and allows nulls.

  • Key Call:
    migrationBuilder.AddColumn<string>(...)

  • Parameters:

    • name: New column name ("CustomerName").
    • schema: Schema of target table ("irby").
    • table: Table name ("Customer").
    • type: SQL data type ("nvarchar(max)").
    • nullable: Whether null values are allowed (true).

Code Snippet:

protected override void Down(MigrationBuilder migrationBuilder)
{
    migrationBuilder.AddColumn<string>(
        name: "CustomerName",
        schema: "irby",
        table: "Customer",
        type: "nvarchar(max)",
        nullable: true);
}

5. Usage & Execution ⚙️

  1. Add Migration:
    dotnet ef migrations add DeleteColumnCustomerTable
    
  2. Review Generated File:
    • Located under Migrations/20221103025134_DeleteColumnCustomerTable.cs.
  3. Apply Migration to Database:
    dotnet ef database update
    
  4. Rollback (if needed):
    dotnet ef database update PreviousMigrationName
    

6. UML Class Diagram 📈

Below is a simple class diagram showing the migration structure:

+-----------------------------------------+
|             Migration                   |
|-----------------------------------------|
| + void Up(MigrationBuilder builder)     |
| + void Down(MigrationBuilder builder)   |
+----------------+------------------------+
                 ^
                 |
+-----------------------------------------+
|  DeleteColumnCustomerTable : Migration  |
|-----------------------------------------|
| <<override>> Up(builder)                |
| <<override>> Down(builder)              |
+-----------------------------------------+
  • Inheritance:
    DeleteColumnCustomerTable inherits from Migration.

7. Conclusion ✅

In this migration file:

  • Up removes the unused CustomerName column, streamlining the schema.
  • Down ensures safe rollback by re-adding the column if necessary.

This pattern (Up/Down) is fundamental to EF Core migrations, enabling iterative, versioned schema evolution in a team-friendly and reversible manner.


🚀 Happy coding with Entity Framework Core migrations!