πŸ“„ Documentation: 20220809153958_UpdateDocImageTable.cs


πŸ“‘ Table of Contents

  1. Overview & Purpose
  2. Key Functional Areas
  3. Dependencies & Technologies
  4. Data Model Changes
  5. Migration Class Structure
  6. Usage Instructions
  7. Code Snippets & Examples
  8. UML Diagram
  9. Conclusion

🌟 1. Overview & Purpose

The UpdateDocImageTable migration modifies the database schema of the DocumentImage table by adding a new column:

  • LegalDocumentType (uniqueidentifier, non-nullable)

This migration ensures that each record within the irby.DocumentImage table can reference a legal document type (via a GUID) within your system.

Why is it needed?

  • Track different categories/types of legal documents associated with an image.
  • Enforce data integrity at the database level by preventing null values for LegalDocumentType.

πŸ”‘ 2. Key Functional Areas

Area Description
Schema Evolution Adds a new column to an existing table without data loss.
Data Integrity Ensures LegalDocumentType is always populated (non-nullable, default GUID = zero).
Rollback Capability Provides a Down method to remove the column if needed.
EF Core Migrations Workflow Integrates into EF Core’s migration pipeline (dotnet ef).

πŸ› οΈ 3. Dependencies & Technologies

  • .NET Core / .NET 5+
  • Entity Framework Core (EF Core)
    • Microsoft.EntityFrameworkCore.Migrations
  • SQL Server (implied by uniqueidentifier type)
  • C# Language Features
    • Generics, partial classes, override methods

Ensure your project has the EF Core tools installed:

dotnet tool install --global dotnet-ef
dotnet add package Microsoft.EntityFrameworkCore.Design

πŸ—„οΈ 4. Data Model Changes

Before this migration, the DocumentImage table had columns such as:

Column Name Data Type Nullable
Id uniqueidentifier No
DocumentId uniqueidentifier No
ImagePath nvarchar(max) Yes
CreatedAt datetime2 No
... ... ...

After applying this migration, the table gains:

Column Name Data Type Nullable Default Value
LegalDocumentType uniqueidentifier No 00000000-0000-0000-0000-000000000000

🚧 5. Migration Class Structure

namespace IrbyImaging.DbContext.Migrations
{
    public partial class UpdateDocImageTable : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder) { /* ... */ }
        protected override void Down(MigrationBuilder migrationBuilder) { /* ... */ }
    }
}

πŸ†™ Up Method

  • Purpose: Apply the schema change.
  • Operation:
    migrationBuilder.AddColumn<Guid>(
        name: "LegalDocumentType",
        schema: "irby",
        table: "DocumentImage",
        type: "uniqueidentifier",
        nullable: false,
        defaultValue: new Guid("00000000-0000-0000-0000-000000000000"));
    
  • Effects:
    • Adds LegalDocumentType as a non-nullable GUID.
    • Sets default to the β€œempty” GUID for all existing rows.

πŸ”½ Down Method

  • Purpose: Roll back the schema change.
  • Operation:
    migrationBuilder.DropColumn(
        name: "LegalDocumentType",
        schema: "irby",
        table: "DocumentImage");
    
  • Effects:
    • Removes the LegalDocumentType column entirely.

πŸš€ 6. Usage Instructions

Applying the Migration

  1. Create Migration (already done):
    dotnet ef migrations add UpdateDocImageTable
    
  2. Apply to Database:
    dotnet ef database update
    
  3. Verify:
    SELECT TOP 1 LegalDocumentType 
    FROM irby.DocumentImage;
    

Reverting the Migration

  1. Rollback One Step:
    dotnet ef database update <PreviousMigrationName>
    
  2. Remove Migration Files (if needed):
    dotnet ef migrations remove
    

πŸ’‘ 7. Code Snippets & Examples

// Adding a DocumentImage with a LegalDocumentType
var docImage = new DocumentImage
{
    Id = Guid.NewGuid(),
    DocumentId = someDocumentId,
    ImagePath = "path/to/image.png",
    CreatedAt = DateTime.UtcNow,
    LegalDocumentType = new Guid("11111111-2222-3333-4444-555555555555")
};

_dbContext.DocumentImages.Add(docImage);
_dbContext.SaveChanges();
-- Query to find all images of a specific legal document type
SELECT *
FROM irby.DocumentImage
WHERE LegalDocumentType = '11111111-2222-3333-4444-555555555555';

πŸ“Š 8. UML Diagram

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

class UpdateDocImageTable {
  + Up(MigrationBuilder)
  + Down(MigrationBuilder)
}

Migration <|-- UpdateDocImageTable
@enduml

βœ… 9. Conclusion

This migration enhances the DocumentImage table by introducing a critical column, LegalDocumentType, ensuring each image is properly categorized by its legal document type. The built-in Up/Down methods provide seamless schema evolution and rollback capabilities using EF Core’s migration framework. Apply it to keep your database in sync with your domain model!


Generated with ❀️ by your Documentation Assistant