📄 Documentation: 20230707160813_AddNewSafetyColumns.cs

File Path: IrbyImaging.DbContext/Migrations/20230707160813_AddNewSafetyColumns.cs


📑 Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Migration Class Structure (UML Diagram)
  4. Key Functional Areas
  5. Schema Changes Details
  6. Usage Examples
  7. Impact on Domain Models
  8. Best Practices & Tips
  9. Conclusion

1. Overview & Purpose

This migration adds a new “safety” flag to several tables in the irby schema, enabling the application to:

  • Mark certain document‐related records as safety‑critical.
  • Associate a type with safety documents (SafetyDocumentType).

🔍 Why this change?
The Irby Imaging solution requires tracking which documents and related metadata are classified as Safety. Introducing these boolean flags and a GUID reference allows flexible querying, filtering, and enforcing business rules around safety documentation.


2. Dependencies & Technologies

Technology / Package Version & Notes
Microsoft.EntityFrameworkCore EF Core provides the Migration base class
Microsoft.EntityFrameworkCore.Migrations Enables code‐based migrations (Up / Down methods)
.NET SDK .NET 6+ / .NET 7+ (implicit from project context)

ℹ️ This file targets Entity Framework Core Migrations. No external third‑party integrations are directly used here.


3. Migration Class Structure (UML Diagram)

+------------------------------------------------+
|         AddNewSafetyColumns : Migration        |
+------------------------------------------------+
| <<public>> override void Up(MigrationBuilder)   |
| <<public>> override void Down(MigrationBuilder) |
+------------------------------------------------+
  1. AddNewSafetyColumns inherits from Migration.
  2. Contains two overrides:
    • Up → Applies schema changes
    • Down → Rolls back schema changes

4. Key Functional Areas

4.1. Up Method

protected override void Up(MigrationBuilder migrationBuilder)
{
    // 1️⃣ Add IsSafety to TempDocumentData
    migrationBuilder.AddColumn<bool>(
        name: "IsSafety", schema: "irby", table: "TempDocumentData",
        type: "bit", nullable: false, defaultValue: false);

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

    // 3️⃣ Add IsSafety & SafetyDocumentType to DocumentImage
    migrationBuilder.AddColumn<bool>(
        name: "IsSafety", schema: "irby", table: "DocumentImage",
        type: "bit", nullable: false, defaultValue: false);
    migrationBuilder.AddColumn<Guid>(
        name: "SafetyDocumentType", schema: "irby", table: "DocumentImage",
        type: "uniqueidentifier", nullable: false, 
        defaultValue: new Guid("00000000-0000-0000-0000-000000000000"));

    // 4️⃣ Add IsSafety to DocumentCategory & DocumentAttribute
    migrationBuilder.AddColumn<bool>(
        name: "IsSafety", schema: "irby", table: "DocumentCategory",
        type: "bit", nullable: false, defaultValue: false);
    migrationBuilder.AddColumn<bool>(
        name: "IsSafety", schema: "irby", table: "DocumentAttribute",
        type: "bit", nullable: false, defaultValue: false);
}
  • Boolean Flags (IsSafety)

    • Added to:
      • TempDocumentData
      • DocumentType
      • DocumentImage
      • DocumentCategory
      • DocumentAttribute
    • Default: false (non‐safety by default)
  • GUID Reference (SafetyDocumentType)

    • Added to:
      • DocumentImage
    • Purpose: Link an image to a specific safety document type
    • Default: 00000000-0000-0000-0000-000000000000 (placeholder)

4.2. Down Method

protected override void Down(MigrationBuilder migrationBuilder)
{
    migrationBuilder.DropColumn(name: "IsSafety", schema: "irby", table: "TempDocumentData");
    migrationBuilder.DropColumn(name: "IsSafety", schema: "irby", table: "DocumentType");
    migrationBuilder.DropColumn(name: "IsSafety", schema: "irby", table: "DocumentImage");
    migrationBuilder.DropColumn(name: "SafetyDocumentType", schema: "irby", table: "DocumentImage");
    migrationBuilder.DropColumn(name: "IsSafety", schema: "irby", table: "DocumentCategory");
    migrationBuilder.DropColumn(name: "IsSafety", schema: "irby", table: "DocumentAttribute");
}
  • Reverts all changes made in Up.
  • Ensures a clean rollback to the previous schema state.

5. Schema Changes Details

Table Name New Column Type Nullable Default Value Purpose
irby.TempDocumentData IsSafety bit No false Flags temp data as safety‐related
irby.DocumentType IsSafety bit No false Flags document types for safety
irby.DocumentImage IsSafety bit No false Flags images related to safety docs
irby.DocumentImage SafetyDocumentType uniqueidentifier No 00000000-0000-0000-0000-000000000000 Link to a safety doc type
irby.DocumentCategory IsSafety bit No false Flags categories for safety docs
irby.DocumentAttribute IsSafety bit No false Flags attributes for safety docs

6. Usage Examples

6.1. Applying the Migration

# 1. Create the migration (already done)
dotnet ef migrations add AddNewSafetyColumns \
    --project IrbyImaging.DbContext \
    --startup-project IrbyImaging.Api

# 2. Update the database
dotnet ef database update \
    --project IrbyImaging.DbContext \
    --startup-project IrbyImaging.Api

✅ Result: New columns are created in the database under the irby schema.

6.2. Reverting the Migration

# Roll back one migration
dotnet ef database update 20230707160812_PreviousMigrationName \
    --project IrbyImaging.DbContext \
    --startup-project IrbyImaging.Api

⏪ Result: Drops all IsSafety flags and SafetyDocumentType column added by this migration.


7. Impact on Domain Models

After applying this migration, be sure to:

  1. Update your EF Core Entity Classes

    • Add corresponding public bool IsSafety { get; set; }
    • Add public Guid SafetyDocumentType { get; set; } to DocumentImage entity.
  2. Modify Business Logic

    • Filter queries by IsSafety == true for safety reports.
    • Enforce rules when creating/updating safety documents.
  3. Adjust DTOs / View Models

    • Expose IsSafety in your API responses, if relevant.
    • Validate non‐nullable GUIDs for SafetyDocumentType.

8. Best Practices & Tips

  • 🔐 Migration Idempotency: Always ensure Down() properly undoes Up().
  • 📦 Default Values: Setting defaults avoids errors on existing rows.
  • 🔄 Testing: Run integration tests after migration to confirm queries work.
  • 📑 Documentation: Keep migration docs in sync with domain models and API specs.

9. Conclusion

This migration enables the Safety feature by introducing flags and references across the document management schema. Once applied, you can:

  • Tag any document and related data as safety‐critical.
  • Associate document images with safety types for downstream processing.

🚀 Next Steps:

  1. Update your C# EF Core models.
  2. Update services/repositories to leverage IsSafety.
  3. Extend your API contracts and UI to display safety metadata.

👍 Happy Coding!
Irby Imaging Team