📦 IrbyImaging.DbContext Migration
20220427190253_HelpDesk_AddTicketNumber.cs
Assembly: IrbyImaging.DbContext
Namespace:IrbyImaging.DbContext.Migrations
Author: kvamshi
Created/Modified: 05‑17‑2022
📋 Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Key Functional Areas
- Data Model Impact
- Code Walkthrough
- UML Diagram
- How to Use / Apply Migration
- Rollback / Undo Migration
- Best Practices & Notes
- Conclusion
1. Overview & Purpose 🚀
This Entity Framework Core migration adds a new column, TicketNumber, to the existing irby.Ticket table.
- Ensures every help‑desk ticket has a unique, sequential identifier.
- Backfills existing records with a default value of
0.
Why?
- Aid in ticket tracking & reporting
- Provide a human‑readable reference for support staff
2. Dependencies & Technologies ⚙️
| Technology / Package | Version (approx.) | Purpose |
|---|---|---|
| Microsoft.EntityFrameworkCore | 5.x / 6.x | Core ORM & migrations framework |
| Microsoft.EntityFrameworkCore.Relational | — | Provides relational‑specific migration operations |
| .NET 5 / 6 SDK | — | Hosting runtime |
3. Key Functional Areas 🔑
- Schema Modification
- Introduce
TicketNumbercolumn (type: INT, NOT NULL, default: 0)
- Introduce
- Migration Runner
Up(...)– applies the schema changeDown(...)– reverts the schema change
4. Data Model Impact 🗄️
Before:
| Schema | Table | Columns |
|---|---|---|
| irby | Ticket | Id (PK), Title, Status… |
After:
| Schema | Table | Columns |
|---|---|---|
| irby | Ticket | Id (PK), Title, Status…, TicketNumber |
- TicketNumber
- SQL Type:
int - Not nullable
- Default value:
0
- SQL Type:
5. Code Walkthrough 🔍
using Microsoft.EntityFrameworkCore.Migrations;
namespace IrbyImaging.DbContext.Migrations
{
public partial class HelpDesk_AddTicketNumber : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "TicketNumber",
schema: "irby",
table: "Ticket",
type: "int",
nullable: false,
defaultValue: 0);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "TicketNumber",
schema: "irby",
table: "Ticket");
}
}
}
Class Definition
| Member | Description |
|---|---|
HelpDesk_AddTicketNumber : Migration |
Partial class that inherits from EF Core’s Migration base |
Up(MigrationBuilder migrationBuilder) |
Defines the upgrade path (add column) |
Down(MigrationBuilder migrationBuilder) |
Defines the downgrade path (remove column) |
Up Method
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "TicketNumber",
schema: "irby",
table: "Ticket",
type: "int",
nullable: false,
defaultValue: 0);
}
AddColumn<int>- name:
"TicketNumber" - schema:
"irby" - table:
"Ticket" - type:
"int" - nullable:
false - defaultValue:
0
- name:
The database engine will backfill existing records with
0.
Down Method
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "TicketNumber",
schema: "irby",
table: "Ticket");
}
- Removes
TicketNumberfromirby.Ticket, restoring the previous schema state.
6. UML Diagram 📈
classDiagram
Migration <|-- HelpDesk_AddTicketNumber
HelpDesk_AddTicketNumber : +Up(MigrationBuilder)
HelpDesk_AddTicketNumber : +Down(MigrationBuilder)
7. How to Use / Apply Migration 🏗️
- Add Migration (already added as file):
dotnet ef migrations add HelpDesk_AddTicketNumber - Update Database:
dotnet ef database update - Verify:
- Check table schema in SQL Server / PostgreSQL / MySQL
- Query:
SELECT TOP 10 Id, TicketNumber FROM irby.Ticket;
8. Rollback / Undo Migration 🔄
To revert this change:
dotnet ef database update [PreviousMigrationName]
- Alternatively, drop the column manually if needed:
ALTER TABLE irby.Ticket DROP COLUMN TicketNumber;
9. Best Practices & Notes 📌
- Default Values
- Ensure
defaultValue: 0aligns with your ticket‑numbering strategy.
- Ensure
- Backfill Strategy
- If existing tickets require real sequence, consider a custom script to assign historical ticket numbers.
- Testing
- Always run migrations in a staging environment before production.
- Version Control
- Migration files should be committed alongside your code changes.
10. Conclusion ✅
This migration is a simple but critical step in the Help Desk domain, enabling explicit ticket numbering at the database level.
By following Entity Framework Core’s migration pattern, you ensure a consistent evolution of your schema, easy rollbacks, and clear intent in version control.
Thank you for using IrbyImaging’s data context migrations! 🎉