📑 IrbyImaging.DbContext Migrations

20230711142328_NewIsHRColumn


📚 Table of Contents

  1. Overview & Purpose
  2. Key Functional Areas
  3. Dependencies & Technologies
  4. Migration Details
  5. Tables & Columns Affected
  6. Usage & Application
  7. UML Diagram
  8. Sample Code & Queries
  9. Conclusion

1. Overview & Purpose

This Entity Framework Core migration NewIsHRColumn 🚀 introduces a new boolean flag (IsHR) to multiple tables in the irby schema, plus a GUID reference (HRDocumentType) on the DocumentImage table.

Why?

  • Mark records that belong to Human Resources workflows
  • Enable filtering/searching on HR‑related documents
  • Provide a foundation for future HR‑specific business logic

2. Key Functional Areas

  • Schema Evolution: Add new columns without losing existing data
  • Forward/Backward Compatibility: Support both Up (apply) and Down (rollback) operations
  • Data Integrity: Default values ensure that legacy rows are non‑disruptive

3. Dependencies & Technologies

Technology Version/Detail
.NET SDK ≥ .NET 6.0
Entity Framework Core ≥ 6.x
Database Provider Microsoft SQL Server (bit, uniqueidentifier)
CLI Tool dotnet ef

4. Migration Details

File path:

IrbyImaging.DbContext/Migrations/20230711142328_NewIsHRColumn.cs

4.1 Up Method

protected override void Up(MigrationBuilder migrationBuilder)
{
    // TempDocumentData: add HR flag
    migrationBuilder.AddColumn<bool>(
        name: "IsHR",
        schema: "irby",
        table: "TempDocumentData",
        type: "bit",
        nullable: false,
        defaultValue: false);

    // DocumentType: add HR flag
    migrationBuilder.AddColumn<bool>(
        name: "IsHR",
        schema: "irby",
        table: "DocumentType",
        type: "bit",
        nullable: false,
        defaultValue: false);

    // DocumentImage: add reference and flag
    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);

    // DocumentCategory: add HR flag
    migrationBuilder.AddColumn<bool>(
        name: "IsHR",
        schema: "irby",
        table: "DocumentCategory",
        type: "bit",
        nullable: false,
        defaultValue: false);

    // DocumentAttribute: add HR flag
    migrationBuilder.AddColumn<bool>(
        name: "IsHR",
        schema: "irby",
        table: "DocumentAttribute",
        type: "bit",
        nullable: false,
        defaultValue: false);
}

4.2 Down Method

protected override void Down(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");
}

5. Tables & Columns Affected

Table Name Column Name Data Type Default Value Purpose
TempDocumentData IsHR bit false Flag temp docs as HR‑related
DocumentType IsHR bit false HR‑flag for doc type lookup
DocumentImage HRDocumentType uniqueidentifier 0000...0000 (Guid.Empty) Link to a special HR doc type
DocumentImage IsHR bit false HR‑flag for images
DocumentCategory IsHR bit false Flag categories used in HR flows
DocumentAttribute IsHR bit false Mark attributes for HR contexts

5.1 Before vs. After Schema

Before:

irby.DocumentImage:
   Id (GUID)
   FileName (nvarchar)
   ... other columns

After:

irby.DocumentImage:
   Id
   FileName
   ... other columns
   ├─ HRDocumentType : uniqueidentifier
   └─ IsHR            : bit

6. Usage & Application

6.1 Applying the Migration 🚀

  1. Add Migration
    dotnet ef migrations add NewIsHRColumn --project IrbyImaging.DbContext
    
  2. Update Database
    dotnet ef database update --project IrbyImaging.DbContext
    
    This will execute the Up method and alter your schema.

6.2 Rolling Back ⏪

If you need to undo:

dotnet ef database update LastGoodMigrationName

This runs the Down method for NewIsHRColumn, removing the new columns.


7. UML Diagram

@startuml
class Migration {
    +Up(MigrationBuilder)
    +Down(MigrationBuilder)
}

class TempDocumentData {
    +bool IsHR
}

class DocumentType {
    +bool IsHR
}

class DocumentImage {
    +Guid HRDocumentType
    +bool IsHR
}

class DocumentCategory {
    +bool IsHR
}

class DocumentAttribute {
    +bool IsHR
}

Migration --> TempDocumentData : adds
Migration --> DocumentType     : adds
Migration --> DocumentImage    : adds
Migration --> DocumentCategory : adds
Migration --> DocumentAttribute: adds
@enduml

8. Sample Code & Queries

// 1. Query all HR‑flagged images
var hrImages = dbContext.DocumentImage
    .Where(img => img.IsHR)
    .ToList();

// 2. Set an existing DocumentType to HR
var docType = dbContext.DocumentType.Find(someId);
docType.IsHR = true;
dbContext.SaveChanges();

// 3. Join HRDocumentType GUID to actual DocumentType entity
var hrJoined = from img in dbContext.DocumentImage
               join dt in dbContext.DocumentType
                 on img.HRDocumentType equals dt.Id
               where img.IsHR
               select new { img.Id, dt.Name };

9. Conclusion 🎉

The NewIsHRColumn migration is a straightforward, yet powerful enhancement that

  • Tags key entities with an HR context
  • Enables filtering & business logic for HR‑specific workflows
  • Maintains full rollback capability

With these new columns in place, your application can now:

  • Distinguish HR documents at the data layer
  • Build HR‑only processing pipelines
  • Extend analytics and reporting on HR assets

✨ End of Documentation for 20230711142328_NewIsHRColumn.cs ✨