๐Ÿ“š IrbyImaging.DbContext Migration Documentation

File: Migrations/20220511205338_UpdateTicketTable.cs
Assembly: IrbyImaging.DbContext
Author: kvamshi
Created/Modified: 05โ€‘17โ€‘2022


๐Ÿ“‘ Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Migration Class Structure
  4. The Up() Method
  5. The Down() Method
  6. Ticket Table Before & After
  7. UML Diagram
  8. Usage Example
  9. Conclusion

๐Ÿ” Overview & Purpose

This migration augments the existing irby.Ticket table by adding an ImageContents column of type varbinary(max).

  • Why? To store raw image data (e.g., photo attachments) directly within the ticket record.
  • Scope:
    • Up: Introduce ImageContents as a nullable column.
    • Down: Remove ImageContents in case of rollback.

โš™๏ธ Dependencies & Technologies

Dependency Version / Details
Microsoft.EntityFrameworkCore EF Core โ‰ฅ 3.x
Microsoft.EntityFrameworkCore.Relational Enables MigrationBuilder methods
.NET Core / .NET 5+ Hosting the DbContext

๐Ÿ› ๏ธ Migration Class Structure

namespace IrbyImaging.DbContext.Migrations
{
    public partial class UpdateTicketTable : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder) { /* ... */ }
        protected override void Down(MigrationBuilder migrationBuilder) { /* ... */ }
    }
}
  • partial class UpdateTicketTable
    • Inherits from Migration.
    • Auto-generated by EF Core when you run Add-Migration UpdateTicketTable.

๐Ÿš€ The Up() Method

Adds a new column to the Ticket table.

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.AddColumn<byte[]>(
        name: "ImageContents",
        schema: "irby",
        table: "Ticket",
        type: "varbinary(max)",
        nullable: true);
}

Key Points:

  • AddColumn<byte[]>: The CLR type is byte[].
  • Schema: irby
  • Table: Ticket
  • SQL Type: varbinary(max)
  • Nullable: true (no existing data break)

๐Ÿฉน The Down() Method

Reverts the change by dropping the ImageContents column.

protected override void Down(MigrationBuilder migrationBuilder)
{
    migrationBuilder.DropColumn(
        name: "ImageContents",
        schema: "irby",
        table: "Ticket");
}

๐Ÿ—ƒ๏ธ Ticket Table Before & After

Column Name Data Type Nullable Comments
Id int NO PK
Title nvarchar(200) NO Ticket title
CreatedDate datetime2 NO When issued
ImageContents varbinary(max) YES New! stores image bytes

๐Ÿ“ UML Diagram

classDiagram
    class Migration {
        <<abstract>>
        +Up(MigrationBuilder)
        +Down(MigrationBuilder)
    }
    class UpdateTicketTable {
        +Up(MigrationBuilder)
        +Down(MigrationBuilder)
    }
    UpdateTicketTable --|> Migration

๐Ÿ’ก Usage Example

  1. Add Migration
    dotnet ef migrations add UpdateTicketTable 
    # or in Package Manager Console:
    # Add-Migration UpdateTicketTable
    
  2. Apply to Database
    dotnet ef database update  
    # or in PMC:
    # Update-Database
    
  3. Verify
    SELECT TOP 1 ImageContents
    FROM irby.Ticket;
    

๐Ÿ Conclusion

This migration seamlessly extends the Ticket entity to hold binary image data, empowering the application to store attachments directly in the database. It follows EF Core best practicesโ€”providing simple Up and Down scriptsโ€”and integrates smoothly into your CI/CD pipeline via standard dotnet ef commands.

Happy migrating! ๐Ÿš€