πŸ“„ IrbyImaging.DbContext Migration

File: 20220613155005_NewColumnDocumentImageTable.cs


πŸ“š Table of Contents

  1. Overview & Purpose
  2. Context: EF Core Migrations
  3. File Structure & Metadata
  4. Key Functional Areas
  5. Schema Changes (Up & Down)
  6. Dependencies & Technologies
  7. Code Walkthrough
    7.1. Up Method πŸš€
    7.2. Down Method πŸ”„
  8. UML Diagram πŸ“ˆ
  9. Usage Examples & Commands
  10. Best Practices & Recommendations
  11. Conclusion

1. πŸ” Overview & Purpose

This migration adds a new column, DocumentIndexes, of type nvarchar(max) to the DocumentImage table in the irby schema.

  • Why?
    • To store additional indexing or tagging information about document images.
    • Enables flexible, text-based indexes for search or categorization features in the application.

2. πŸ— Context: EF Core Migrations

Entity Framework Core (EF Core) Migrations provide a way to incrementally evolve your database schema alongside your application’s data model. Each migration class:

  • Derives from Microsoft.EntityFrameworkCore.Migrations.Migration
  • Implements two methods:
    • Up(...) – apply changes
    • Down(...) – revert changes

3. πŸ—‚ File Structure & Metadata

Section Details
Assembly IrbyImaging.DbContext
Author kvamshi
Created On 06-13-2022
Last Modified On 06-13-2022
Namespace IrbyImaging.DbContext.Migrations
Class NewColumnDocumentImageTable
Inherits Migration
// Assembly      : IrbyImaging.DbContext
// Author        : kvamshi
// Created       : 06-13-2022
// Last Modified : 06-13-2022

4. 🚩 Key Functional Areas

  • Schema Modification
    • Add Column DocumentIndexes
    • Drop Column DocumentIndexes
  • Version Control
    • This migration has a timestamp prefix: 20220613155005_
  • Rollback Capability
    • Provides a Down method for safe reverting

5. πŸ”„ Schema Changes (Up & Down)

Method Operation Schema Table Column Type Nullable
Up AddColumn irby DocumentImage DocumentIndexes nvarchar(max) Yes
Down DropColumn irby DocumentImage DocumentIndexes – –

6. πŸ›  Dependencies & Technologies

  • .NET / C#
  • Entity Framework Core
    • Microsoft.EntityFrameworkCore.Migrations
  • Database
    • SQL Server (nvarchar(max))
  • Development Tools
    • dotnet ef CLI for migration commands

7. πŸ“– Code Walkthrough

using Microsoft.EntityFrameworkCore.Migrations;

namespace IrbyImaging.DbContext.Migrations
{
    /// <summary>
    /// Adds a new column 'DocumentIndexes' to DocumentImage table.
    /// </summary>
    public partial class NewColumnDocumentImageTable : Migration
    {
        /// <summary>
        /// Apply schema changes (Up).
        /// </summary>
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<string>(
                name: "DocumentIndexes",
                schema: "irby",
                table: "DocumentImage",
                type: "nvarchar(max)",
                nullable: true);
        }

        /// <summary>
        /// Revert schema changes (Down).
        /// </summary>
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "DocumentIndexes",
                schema: "irby",
                table: "DocumentImage");
        }
    }
}

7.1 Up Method πŸš€

  • Purpose:
    Creates a new nullable column DocumentIndexes on the irby.DocumentImage table.
  • Key API Used:
    migrationBuilder.AddColumn<string>(
        name: "DocumentIndexes",
        schema: "irby",
        table: "DocumentImage",
        type: "nvarchar(max)",
        nullable: true);
    
  • Result:
    Updates database DDL to include the new column.

7.2 Down Method πŸ”„

  • Purpose:
    Safely removes the DocumentIndexes column if rolling back.
  • Key API Used:
    migrationBuilder.DropColumn(
        name: "DocumentIndexes",
        schema: "irby",
        table: "DocumentImage");
    
  • Result:
    Database schema reverts to its prior state.

8. πŸ“ˆ UML Diagram

@startuml
package IrbyImaging.DbContext.Migrations {
  class NewColumnDocumentImageTable {
    +Up(MigrationBuilder)
    +Down(MigrationBuilder)
  }
  class Migration {
    <<abstract>>
    +Up(MigrationBuilder)
    +Down(MigrationBuilder)
  }
  NewColumnDocumentImageTable --|> Migration
}
@enduml

9. πŸš€ Usage Examples & Commands

  1. Add Migration
    dotnet ef migrations add NewColumnDocumentImageTable
    
  2. Update Database
    dotnet ef database update
    
  3. Rollback (to previous migration)
    dotnet ef database update <PreviousMigrationName>
    

10. πŸ“Œ Best Practices & Recommendations

  • Naming Conventions
    • Prefix with timestamp: YYYYMMDDHHMMSS_DescriptiveName
  • Idempotency
    • Always implement both Up and Down methods.
  • Testing Migrations
    • Test on a staging or local dev database before production.
  • Schema Validation
    • Use EF Core’s EnsureCreated or EnsureDeleted for integration tests.

11. βœ… Conclusion

This migration seamlessly extends the DocumentImage table to support flexible, text-based indexes via the new DocumentIndexes column. With clear Up and Down implementations, you can evolve your schema safely and roll back if needed.

Feel free to integrate, test, and apply this migration in your CI/CD pipeline. Happy coding! πŸš€