📋 Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Key Functional Areas
  4. Data Model Impact
  5. How It Works
  6. Usage & Examples
  7. UML Diagrams
  8. Error Handling & Migration Rollback
  9. Conclusion 🎉

Overview & Purpose

File
IrbyImaging.DbContext/Migrations/20230718150337_AddUARApproverToEmployeeTable.cs

This Entity Framework Core migration adds a new boolean flag, UARApprover, to the Employee table in the irby schema.

🔑 Why?
In the Irby Imaging domain, certain employees are designated as UAR (User Access Request) approvers. This flag enables the application to programmatically determine who has the authority to approve user access requests.


Dependencies & Technologies

Technology / Package Version / Note
.NET Core / .NET 6+ C# language, project target framework
Microsoft.EntityFrameworkCore For migrations, DbContext, MigrationBuilder
irby DB schema Custom schema within SQL Server / PostgreSQL

⚙️ Key Namespace
using Microsoft.EntityFrameworkCore.Migrations;


Key Functional Areas

  1. Migration Class Declaration
    • public partial class AddUARApproverToEmployeeTable : Migration
  2. Up Method
    • Defines how the database schema changes when the migration is applied.
    • Adds a new column UARApprover (boolean) to the Employee table.
  3. Down Method
    • Defines how to revert the changes if the migration is rolled back.
    • Drops the UARApprover column from the Employee table.

Data Model Impact

Table New Column Type Nullable Default Value Description
Employee UARApprover bit No false Indicates if employee can approve UARs.

💡 Note:
After applying this migration, every existing record in Employee will have UARApprover = false by default.


How It Works

public partial class AddUARApproverToEmployeeTable : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        // 1️⃣ Add the UARApprover column (bool) to Employee in irby schema
        migrationBuilder.AddColumn<bool>(
            name: "UARApprover",
            schema: "irby",
            table: "Employee",
            type: "bit",
            nullable: false,
            defaultValue: false);
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        // 2️⃣ Remove the UARApprover column if rolling back
        migrationBuilder.DropColumn(
            name: "UARApprover",
            schema: "irby",
            table: "Employee");
    }
}
  • Up
    • Uses migrationBuilder.AddColumn<T>() to extend the schema.
  • Down
    • Uses migrationBuilder.DropColumn() to undo the change.

These methods form the forward and backward steps of your version-controlled database schema.


Usage & Examples

1. Generating the Migration

dotnet ef migrations add AddUARApproverToEmployeeTable \
    --project IrbyImaging.DbContext \
    --output-dir Migrations

2. Applying to Database

dotnet ef database update --project IrbyImaging.DbContext

3. Accessing the New Property in Code

public class EmployeeService
{
    private readonly IrbyDbContext _context;

    public EmployeeService(IrbyDbContext context)
    {
        _context = context;
    }

    public async Task<bool> CanApproveUAR(int employeeId)
    {
        var employee = await _context.Employees.FindAsync(employeeId);
        return employee?.UARApprover ?? false;
    }
}

UML Diagrams

Class Diagram (Mermaid)

classDiagram
    class Employee {
        +int EmployeeId
        +string FirstName
        +string LastName
        +bool UARApprover   <<new>>
    }

Migration Flow

flowchart LR
    A[(Start)] --> B[Apply Migrations]
    B --> C[AddColumn UARApprover]
    C --> D[(Database Updated)]
    D --> E{Rollback?}
    E -- Yes --> F[DropColumn UARApprover]
    E -- No --> G[(End)]

Error Handling & Migration Rollback

  • Migration Conflicts

    • If another migration has modified the Employee table concurrently, you may encounter conflicts.
    • Resolution: Re-scaffold the conflicting migration or manually adjust Up / Down scripts.
  • Rollback

    • To rollback one step:
      dotnet ef database update <PreviousMigrationName>
      
    • The Down method will remove UARApprover.

Conclusion 🎉

This migration is a lightweight, zero-downtime update to your Employee data model—adding the UARApprover flag to empower new business logic around user access request approvals.

  • Seamlessly applies via EF Core CLI
  • Safe rollback with the Down method
  • Enables clean, type-safe access in your C# services

By following the Usage & Examples section, you can integrate this schema change into your continuous delivery pipeline and update your application logic to leverage the new flag.


Enjoy the new capability and happy coding! 🚀