πŸ“‘ Documentation: 20230802163348_RemoveColumnFromRole.cs


πŸ“‹ Table of Contents

  1. πŸ“ Overview & Purpose
  2. πŸ› οΈ Dependencies & Technologies
  3. πŸ“Š View Models & Data Models
  4. 🎯 Key Functional Areas
  5. πŸ”§ Helper Methods & Utilities
  6. πŸ” Detailed Code Walkthrough
  7. πŸ›‘οΈ Error Handling & Rollback Mechanisms
  8. 🚫 API Endpoints Overview
  9. πŸ”— Third‑party Integrations
  10. πŸ“ˆ UML Diagram
  11. πŸ’‘ Usage Examples & Code Snippets
  12. βœ… Conclusion

πŸ“ Overview & Purpose

This EF Core migration was generated to remove the DepartmentId column (alongside its associated foreign key and index) from the Role table in the irby schema.

Why is this needed?

  • 🎯 Business rule change: Roles are no longer tied to Departments.
  • πŸ—‘οΈ Simplify the data model by removing obsolete relationships.

Impact at a Glance

Operation Schema Table Target Column
Drop Foreign Key irby Role DepartmentId
Drop Index irby Role IX_Role_DepartmentId
Drop Column irby Role DepartmentId

πŸ› οΈ Dependencies & Technologies

  • .NET SDK: 6.0 / 7.0
  • Entity Framework Core
    • Microsoft.EntityFrameworkCore
    • Microsoft.EntityFrameworkCore.Migrations
  • Database Provider: SQL Server (or any EF Core–supported RDBMS)

πŸ“Š View Models & Data Models

Not Applicable
This file is exclusively a schema migration. No view models, DTOs, or domain models are directly defined here.


🎯 Key Functional Areas

  1. Up Migration: Defines the steps to apply (remove) the schema changes.
  2. Down Migration: Defines the steps to rollback (revert) the changes if needed.
  3. MigrationBuilder API: EF Core’s fluent methods that translate to SQL commands.

πŸ”§ Helper Methods & Utilities

  • MigrationBuilder:
    • DropForeignKey(...)
    • DropIndex(...)
    • DropColumn(...)
    • AddColumn<T>(...)
    • CreateIndex(...)
    • AddForeignKey(...)

These methods generate the appropriate DDL (Data Definition Language) statements under the hood.


πŸ” Detailed Code Walkthrough

using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace IrbyImaging.DbContext.Migrations
{
    /// <inheritdoc />
    public partial class RemoveColumnFromRole : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            // ... see below
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            // ... see below
        }
    }
}

βš™οΈ Up Method

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.DropForeignKey(
        name: "FK_Role_Department_DepartmentId",
        schema: "irby",
        table: "Role");

    migrationBuilder.DropIndex(
        name: "IX_Role_DepartmentId",
        schema: "irby",
        table: "Role");

    migrationBuilder.DropColumn(
        name: "DepartmentId",
        schema: "irby",
        table: "Role");
}
  • DropForeignKey:
    Removes the foreign key constraint FK_Role_Department_DepartmentId.
  • DropIndex:
    Deletes the supporting index IX_Role_DepartmentId for faster lookups.
  • DropColumn:
    Eliminates the DepartmentId column from irby.Role.

βͺ Down Method

protected override void Down(MigrationBuilder migrationBuilder)
{
    migrationBuilder.AddColumn<Guid>(
        name: "DepartmentId",
        schema: "irby",
        table: "Role",
        type: "uniqueidentifier",
        nullable: true);

    migrationBuilder.CreateIndex(
        name: "IX_Role_DepartmentId",
        schema: "irby",
        table: "Role",
        column: "DepartmentId");

    migrationBuilder.AddForeignKey(
        name: "FK_Role_Department_DepartmentId",
        schema: "irby",
        table: "Role",
        column: "DepartmentId",
        principalSchema: "irby",
        principalTable: "Department",
        principalColumn: "DepartmentId");
}
  • AddColumn:
    Recreates DepartmentId as a nullable uniqueidentifier (Guid).
  • CreateIndex:
    Reestablishes IX_Role_DepartmentId.
  • AddForeignKey:
    Restores the referential link to irby.Department(DepartmentId).

πŸ›‘οΈ Error Handling & Rollback Mechanisms

  • Transactional Safety: EF Core wraps each migration in a transaction. If any command fails, all changes roll back automatically.
  • Manual Rollback:
    • Inspect or manually update the __EFMigrationsHistory table.
    • Re-run dotnet ef database update [PreviousMigration] to revert.

🚫 API Endpoints Overview

Not Applicable
This migration does not define or modify any Web/API endpoints.


πŸ”— Third‑party Integrations

  • Entity Framework Core:
    Utilizes EF Core’s migration system to generate and apply DDL changes.

πŸ“ˆ UML Diagram

classDiagram
    class Migration {
      <<Abstract>>
      +Up(MigrationBuilder)
      +Down(MigrationBuilder)
    }
    class RemoveColumnFromRole {
      +Up(MigrationBuilder)
      +Down(MigrationBuilder)
    }
    Migration <|-- RemoveColumnFromRole

πŸ’‘ Usage Examples & Code Snippets

  1. Apply This Migration
    dotnet ef database update 20230802163348_RemoveColumnFromRole
    
  2. Rollback to Previous State
    dotnet ef database update [NameOfPreviousMigration]
    
  3. List All Migrations
    dotnet ef migrations list
    

Equivalent Raw SQL (for SQL Server)

ALTER TABLE [irby].[Role]
  DROP CONSTRAINT [FK_Role_Department_DepartmentId];

DROP INDEX [IX_Role_DepartmentId] ON [irby].[Role];

ALTER TABLE [irby].[Role] 
  DROP COLUMN [DepartmentId];

βœ… Conclusion

The RemoveColumnFromRole migration decouples the Role entity from the Department entity by removing the DepartmentId column and its associated index and foreign key. This aligns the schema with updated business logic that treats roles independently.

πŸš€ Pro Tip: Always run migrations first in a staging or development environment to validate changes before production deployment.


Happy Coding! πŸŽ‰