πŸ“š Documentation: IrbyImaging.DbContext/Migrations/20220622145439_AddNewCategoryTable.cs


πŸ“– Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Key Functional Areas
  4. Data Models & Schema Changes
  5. UML Diagram
  6. Usage & Examples
  7. Error Handling & Return Types
  8. API Endpoints Overview
  9. Helper Methods & Utilities
  10. Third‑party Integrations
  11. Conclusion 🎯

1. πŸ” Overview & Purpose

This Entity Framework Core migration introduces a new table named DocumentCategory and adds a DocumentCategory column to the existing DocumentType table within the irby schema.

  • Filename: 20220622145439_AddNewCategoryTable.cs
  • Namespace: IrbyImaging.DbContext.Migrations
  • Created/Modified: 06-22-2022 by kvamshi

Why?

  • To categorize documents in the system with richer metadata (name, description, availability flags, audit fields).
  • Maintain backwards compatibility: existing DocumentType records get a new column (DocumentCategory) to link or describe categories.

2. βš™οΈ Dependencies & Technologies

Technology/Package Version/Details
.NET Core / .NET 5+ Entity Framework Core migrations
Microsoft.EntityFrameworkCore Microsoft.EntityFrameworkCore 5.x+
SQL Server uniqueidentifier, nvarchar(max), bit, datetime2

3. πŸ”‘ Key Functional Areas

Up Method Operations πŸš€

protected override void Up(MigrationBuilder migrationBuilder)
{
    // 1. Add 'DocumentCategory' column to 'DocumentType' table
    migrationBuilder.AddColumn<string>(
        name: "DocumentCategory",
        schema: "irby",
        table: "DocumentType",
        type: "nvarchar(max)",
        nullable: true);

    // 2. Create new 'DocumentCategory' table
    migrationBuilder.CreateTable(
        name: "DocumentCategory",
        schema: "irby",
        columns: table => new
        {
            Id            = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
            Name          = table.Column<string>(type: "nvarchar(max)", nullable: true),
            Description   = table.Column<string>(type: "nvarchar(max)", nullable: true),
            IsAvailable   = table.Column<bool>(type: "bit", nullable: false),
            IsDeleted     = table.Column<bool>(type: "bit", nullable: false),
            AddedBy       = table.Column<string>(type: "nvarchar(max)", nullable: true),
            AddedDate     = table.Column<DateTime>(type: "datetime2", nullable: false),
            ModifiedBy    = table.Column<string>(type: "nvarchar(max)", nullable: true),
            ModifiedDate  = table.Column<DateTime>(type: "datetime2", nullable: false)
        },
        constraints: table =>
        {
            table.PrimaryKey("PK_DocumentCategory", x => x.Id);
        });
}
  1. AddColumn

    • Table: irby.DocumentType
    • New Column: DocumentCategory (string, nullable)
  2. CreateTable

    • Table: irby.DocumentCategory
    • Columns:
      • Id (GUID, PK)
      • Name, Description (nvarchar)
      • Flags: IsAvailable, IsDeleted
      • Audit: AddedBy, AddedDate, ModifiedBy, ModifiedDate

Down Method Operations πŸ”„

protected override void Down(MigrationBuilder migrationBuilder)
{
    // 1. Drop 'DocumentCategory' table
    migrationBuilder.DropTable(
        name: "DocumentCategory",
        schema: "irby");

    // 2. Remove 'DocumentCategory' column from 'DocumentType'
    migrationBuilder.DropColumn(
        name: "DocumentCategory",
        schema: "irby",
        table: "DocumentType");
}
  • Rolls back all schema changes introduced in Up().

4. πŸ—ƒοΈ Data Models & Schema Changes

DocumentCategory Table

Column Type Nullable Description
Id uniqueidentifier No Primary Key
Name nvarchar(max) Yes Category display name
Description nvarchar(max) Yes Category details/notes
IsAvailable bit (bool) No Indicates if category is active
IsDeleted bit (bool) No Soft-delete flag
AddedBy nvarchar(max) Yes User/Service that created record
AddedDate datetime2 No Timestamp when created
ModifiedBy nvarchar(max) Yes Last modifier
ModifiedDate datetime2 No Timestamp when modified

DocumentType Table Alteration

Table Column Name Type Nullable Purpose
irby.DocumentType DocumentCategory nvarchar(max) Yes Link/reference to category name/ID

5. πŸ“ˆ UML Diagram

classDiagram
    class DocumentCategory {
        +Guid Id
        +string Name
        +string Description
        +bool IsAvailable
        +bool IsDeleted
        +string AddedBy
        +DateTime AddedDate
        +string ModifiedBy
        +DateTime ModifiedDate
    }
    class DocumentType {
        +Guid Id
        +string TypeName
        +string DocumentCategory   // newly added
        // ... other existing fields
    }
    DocumentType : DocumentCategory (string)

6. πŸš€ Usage & Examples

  1. Add Migration

    dotnet ef migrations add AddNewCategoryTable \
      --project IrbyImaging.DbContext \
      --context IrbyImagingDbContext
    
  2. Apply Migration

    dotnet ef database update \
      --project IrbyImaging.DbContext \
      --context IrbyImagingDbContext
    
  3. Inspect Database

    • Confirm irby.DocumentCategory table is created.
    • Confirm DocumentType.DocumentCategory column exists.

7. πŸ› οΈ Error Handling & Return Types

  • Migrations do not return values or throw exceptions at build-time.
  • Runtime errors may occur if the target schema doesn’t exist or permissions are insufficient.
  • Ensure Idempotency by running Down() before re-applying migration if needed.

8. πŸ”— API Endpoints Overview

Not applicable: This file is strictly a database migration. There are no direct API endpoints.


9. 🧩 Helper Methods & Utilities

Not applicable: All logic is contained within the Up() and Down() override methods of the EF Core Migration base class.


10. πŸ”Œ Third‑party Integrations

  • Entity Framework Core: uses built-in MigrationBuilder.
  • No external NuGet packages beyond EF Core.

11. 🎯 Conclusion

This migration cleanly extends the existing database schema to support document categorization.

  • Forward migration adds a new table and column.
  • Rollback removes them, preserving backward compatibility.

By following the Usage & Examples, teams can seamlessly integrate this change into CI/CD pipelines, ensuring data consistency across environments. πŸŽ‰


Happy coding & happy categorizing! πŸš€