๐ IrbyImaging.DbContext Migration Documentation
File: Migrations/20220511205338_UpdateTicketTable.cs
Assembly: IrbyImaging.DbContext
Author: kvamshi
Created/Modified: 05โ17โ2022
๐ Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Migration Class Structure
- The Up() Method
- The Down() Method
- Ticket Table Before & After
- UML Diagram
- Usage Example
- 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
ImageContentsas a nullable column. - Down: Remove
ImageContentsin case of rollback.
- Up: Introduce
โ๏ธ 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.
- Inherits from
๐ 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 isbyte[].- 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
- Add Migration
dotnet ef migrations add UpdateTicketTable # or in Package Manager Console: # Add-Migration UpdateTicketTable - Apply to Database
dotnet ef database update # or in PMC: # Update-Database - 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! ๐