📦 IrbyImaging.DbContext.Migrations.RemoveHrColumns Migration Documentation


📋 Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Key Functional Areas
  4. Schema Impact & Data Models
  5. Code Walkthrough
  6. Usage & Examples
  7. Considerations & Best Practices
  8. Conclusion

🔍 Overview & Purpose

The RemoveHrColumns migration is designed to clean up deprecated “HR-related” columns across several database tables in the irby schema. Over time, the application’s domain model evolved, and the flags/fields related to “HR” (Human Resources) are no longer needed. This migration:

  • ❌ Drops the IsHr and IsHR boolean flags
  • ❌ Drops the HRDocumentType GUID reference

from the following tables:

Table Name Schema Columns Removed
TempDocumentData irby IsHr
DocumentType irby IsHr
DocumentImage irby HRDocumentType, IsHR
DocumentCategory irby IsHr
DocumentAttribute irby IsHr

This streamlines the schema and prevents unused fields from cluttering the data model.


🛠️ Dependencies & Technologies

  • .NET & C#
  • Entity Framework Core
    • Microsoft.EntityFrameworkCore (Migrations API)
  • SQL Server (or any EF Core‑supported RDBMS)
  • Schema: irby

⚙️ Key Functional Areas

Up Method

The Up method implements the forward schema change:

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.DropColumn(
        name: "IsHr",
        schema: "irby",
        table: "TempDocumentData");

    migrationBuilder.DropColumn(
        name: "IsHr",
        schema: "irby",
        table: "DocumentType");

    migrationBuilder.DropColumn(
        name: "HRDocumentType",
        schema: "irby",
        table: "DocumentImage");

    migrationBuilder.DropColumn(
        name: "IsHR",
        schema: "irby",
        table: "DocumentImage");

    migrationBuilder.DropColumn(
        name: "IsHr",
        schema: "irby",
        table: "DocumentCategory");

    migrationBuilder.DropColumn(
        name: "IsHr",
        schema: "irby",
        table: "DocumentAttribute");
}

What it does

  • Uses migrationBuilder.DropColumn to remove each column.
  • Targets the irby schema explicitly.
  • Propagates removal across multiple tables in one transaction.

Down Method

The Down method reverts the changes, ensuring the migration is reversible:

protected override void Down(MigrationBuilder migrationBuilder)
{
    migrationBuilder.AddColumn<bool>(
        name: "IsHr",
        schema: "irby",
        table: "TempDocumentData",
        type: "bit",
        nullable: false,
        defaultValue: false);

    migrationBuilder.AddColumn<bool>(
        name: "IsHr",
        schema: "irby",
        table: "DocumentType",
        type: "bit",
        nullable: false,
        defaultValue: false);

    migrationBuilder.AddColumn<Guid>(
        name: "HRDocumentType",
        schema: "irby",
        table: "DocumentImage",
        type: "uniqueidentifier",
        nullable: false,
        defaultValue: Guid.Empty);

    migrationBuilder.AddColumn<bool>(
        name: "IsHR",
        schema: "irby",
        table: "DocumentImage",
        type: "bit",
        nullable: false,
        defaultValue: false);

    migrationBuilder.AddColumn<bool>(
        name: "IsHr",
        schema: "irby",
        table: "DocumentCategory",
        type: "bit",
        nullable: false,
        defaultValue: false);

    migrationBuilder.AddColumn<bool>(
        name: "IsHr",
        schema: "irby",
        table: "DocumentAttribute",
        type: "bit",
        nullable: false,
        defaultValue: false);
}

What it does

  • Recreates each boolean column with defaultValue: false.
  • Recreates HRDocumentType with defaultValue: Guid.Empty.
  • Maintains schema fidelity if rolling back.

🗃️ Schema Impact & Data Models

Before & After UML Diagram

erDiagram
    TEMP_DOCUMENT_DATA {
        Guid Id PK
        bool IsHr
        /* ... other fields ... */
    }
    DOCUMENT_TYPE {
        Guid Id PK
        string Name
        bool IsHr
        /* ... other fields ... */
    }
    DOCUMENT_IMAGE {
        Guid Id PK
        Guid HRDocumentType
        bool IsHR
        /* ... other fields ... */
    }
    DOCUMENT_CATEGORY {
        Guid Id PK
        bool IsHr
    }
    DOCUMENT_ATTRIBUTE {
        Guid Id PK
        bool IsHr
    }

    %% AFTER MIGRATION: columns are removed %%
  • All IsHr / IsHR flags no longer appear.
  • The HRDocumentType FK is dropped.

🔍 Code Walkthrough

using System;
using Microsoft.EntityFrameworkCore.Migrations;

namespace IrbyImaging.DbContext.Migrations
{
    public partial class RemoveHrColumns : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            // Remove all deprecated HR columns from each table
            migrationBuilder.DropColumn(name: "IsHr", schema: "irby", table: "TempDocumentData");
            /* ... repeated for other tables ... */
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            // Add them back with default values to reverse the migration
            migrationBuilder.AddColumn<bool>(name: "IsHr", schema: "irby", table: "TempDocumentData", type: "bit", nullable: false, defaultValue: false);
            /* ... repeated for other tables ... */
        }
    }
}
  • MigrationBuilder is the EF Core object for building schema changes.
  • Each DropColumn/AddColumn call is atomic and safe.
  • The Down guard ensures reversibility.

🚀 Usage & Examples

1. Adding the Migration

Via .NET CLI:

dotnet ef migrations add RemoveHrColumns \
    --context IrbyDbContext \
    --project IrbyImaging.DbContext

Via Package Manager Console (Visual Studio):

Add-Migration RemoveHrColumns -Context IrbyDbContext -Project IrbyImaging.DbContext

2. Applying the Migration

dotnet ef database update --context IrbyDbContext
  • This executes the Up method.
  • To rollback, specify the previous migration name:
dotnet ef database update <PreviousMigrationName> --context IrbyDbContext

📝 Considerations & Best Practices

  • Back up production data before schema-structural migrations.
  • Verify foreign keys: dropping columns may break FKs or indexes.
  • Test in your staging environment.
  • Remove any code references (models, view models, services) to these columns to avoid runtime errors.
  • When rolling back, existing data in re-added columns will default to false or Guid.Empty.

🏁 Conclusion

The RemoveHrColumns migration is a straightforward, safe cleanup of obsolete HR-related flags and references across multiple tables in the irby schema. By following the reversible pattern—with well-defined Up and Down methods—this migration upholds database integrity and provides an easy rollback path. Remember to synchronize your domain models and services after applying it to maintain a consistent codebase.


Happy coding & robust database management! ✨