๐Ÿ—’๏ธ IrbyImaging.DbContext Migration

File: IrbyImaging.DbContext/Migrations/20230710174948_AddIsHrColumn.cs
Purpose: Adds IsHr flags and HrDocumentType to documentโ€related tables.


๐Ÿ“‘ Table of Contents

  1. Overview & Purpose
  2. Schema Changes
  3. Migration Details
  4. Dependencies & Technologies
  5. Data Models Affected
  6. Usage & Execution
  7. Rollback (Down Method)
  8. UML Diagram of Changes
  9. Sample CLI & Code Examples
  10. Conclusion

๐Ÿš€ 1. Overview & Purpose

This Entity Framework Core migration adds a new boolean flag (IsHr) to several tables under the irby schema and introduces a GUID column (HrDocumentType) to the DocumentImage table.

  • Why? To mark whether a document entity or category is related to Human Resources (HR) processes.
  • When? During database evolution when the application needs to differentiate HR-related documents for filtering, security, or business logic.

๐Ÿ› ๏ธ 2. Schema Changes

Table Name New Column Type Default Value Nullable?
TempDocumentData IsHr bit (bool) false No
DocumentType IsHr bit (bool) false No
DocumentImage IsHr bit (bool) false No
DocumentImage HrDocumentType uniqueidentifier 00000000-0000-0000-0000-000000000000 No
DocumentCategory IsHr bit (bool) false No
DocumentAttribute IsHr bit (bool) false No

๐Ÿ” 3. Migration Details

namespace IrbyImaging.DbContext.Migrations
{
    public partial class AddIsHrColumn : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            // Add IsHr to TempDocumentData
            migrationBuilder.AddColumn<bool>(
                name: "IsHr",
                schema: "irby",
                table: "TempDocumentData",
                type: "bit",
                nullable: false,
                defaultValue: false);

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

            // Add columns to DocumentImage
            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);

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

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

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            // Reverse additions
            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");
        }
    }
}

Key Points:

  • Up() applies schema changes.
  • Down() reverts them, ensuring safe rollbacks.

๐Ÿ“ฆ 4. Dependencies & Technologies

  • .NET 6+ / .NET Core
  • Entity Framework Core (Microsoft.EntityFrameworkCore.Migrations)
  • SQL Server (implicit, due to bit and uniqueidentifier types)
  • C# 10 (nullable enabled)

๐Ÿ—„๏ธ 5. Data Models Affected

Although the migration file itself doesnโ€™t contain the DbSet or entity definitions, these tables align with C# models such as:

public class DocumentType
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public bool IsHr { get; set; } // <โ€” New property
}

public class DocumentImage
{
    public Guid Id { get; set; }
    public byte[] Data { get; set; }
    public bool IsHr { get; set; }           // <โ€” New property
    public Guid HrDocumentType { get; set; } // <โ€” New property
}

Tip: Update your EF Core model classes to include the new properties before adding or applying the migration.


โœ… 6. Usage & Execution

  1. Add the migration

    dotnet ef migrations add AddIsHrColumn
    
  2. Apply to database

    dotnet ef database update
    
  3. Verify

    • Inspect the database:
      SELECT TOP 5 IsHr FROM irby.DocumentType;
      
    • Ensure default IsHr = 0 (false).

๐Ÿ”„ 7. Rollback (Down Method)

If something goes wrong, roll back to the previous state:

dotnet ef database update <PreviousMigrationName>

This invokes the Down() method and drops all IsHr and HrDocumentType columns.


๐Ÿ“ˆ 8. UML Diagram of Changes

@startuml
class TempDocumentData {
  +bool IsHr
}

class DocumentType {
  +bool IsHr
}

class DocumentImage {
  +bool IsHr
  +Guid HrDocumentType
}

class DocumentCategory {
  +bool IsHr
}

class DocumentAttribute {
  +bool IsHr
}
@enduml

Illustrates the new properties in each entity.


๐Ÿ”ง 9. Sample CLI & Code Examples

Example: Creating a new HR document

var newDocType = new DocumentType {
    Id = Guid.NewGuid(),
    Name = "Employee Handbook",
    IsHr = true
};
dbContext.DocumentTypes.Add(newDocType);
dbContext.SaveChanges();

Filtering HR documents

var hrDocs = dbContext.DocumentImage
    .Where(di => di.IsHr)
    .ToList();

๐ŸŽ‰ 10. Conclusion

With AddIsHrColumn, the database now supports toggling the HRโ€related status of documents, types, categories, and attributes. This enhancement lays the groundwork for HR-specific features like:

  • Access Control
  • Reporting/Analytics
  • Document Workflows

๐Ÿš€ After deploying, remember to update your application code to leverage the new IsHr flags and HrDocumentType for richer, more secure HR functionality!


End of Documentation

IrbyImaging | Migration & Data Layer | ยฉ 2023