π IrbyImaging.DbContext Migration: 20220303192042_AddFileandJobName.cs
π Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- UML Class Diagram
- Key Functional Areas
- 4.1 Up() Method
- 4.2 Down() Method
- Data Model Changes
- Usage & Examples
- Error Handling & Return Types
- ThirdβParty Integrations
- Conclusion
π― Overview & Purpose
This migration adds a new column called JobName to the irby.BillingInvoice table in the database schema. It is part of the Entity Framework Core code-first approach used by the IrbyImaging.DbContext assembly.
- Author: kvamshi
- Created: 03β31β2022
- Last Modified: 03β31β2022
Why this migration?
- To store the name of a related βjobβ or process alongside each invoice record.
- Enables applications to query and filter invoices by this new field.
π Dependencies & Technologies
| Technology / Package | Version / Details |
|---|---|
| .NET / C# | .NET 5+ / C# 9 |
| EntityFrameworkCore | EF Core Migrations (Microsoft.EntityFrameworkCore.Migrations) |
| SQL Server (assumed) | nvarchar(max) column type |
Note: The migration lives under the
irbyschema and affects theBillingInvoicetable.
π UML Class Diagram
+-------------------------------------+
| Microsoft.EntityFrameworkCore |
| .Migrations.Migration |
+-------------------------------------+
β²
β
+-------------------------------------+
| AddFileandJobName : Migration |
|-------------------------------------|
| + Up(mb: MigrationBuilder) : void |
| + Down(mb: MigrationBuilder) : void |
+-------------------------------------+
- Inheritance:
AddFileandJobNameextendsMigration.
π Key Functional Areas
4.1 Up() Method
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "JobName",
schema: "irby",
table: "BillingInvoice",
type: "nvarchar(max)",
nullable: true);
}
- Purpose: Defines the database schema changes to apply when migrating up.
- Action:
- Adds
JobNamecolumn of typenvarchar(max). - Allows NULL values by default.
- Adds
4.2 Down() Method
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "JobName",
schema: "irby",
table: "BillingInvoice");
}
- Purpose: Reverts the changes made in
Up()when rolling back this migration. - Action:
- Drops the
JobNamecolumn fromirby.BillingInvoice.
- Drops the
π Data Model Changes
| Table | Schema | Column | Type | Nullable | Action |
|---|---|---|---|---|---|
| BillingInvoice | irby | JobName | nvarchar(max) | Yes | Add |
- Before Migration: Table
BillingInvoicehad noJobNamecolumn. - After Migration: New column present to capture job name details.
π Usage & Examples
1. Add & Apply Migration
# Create the migration (if not already generated)
dotnet ef migrations add AddFileandJobName \
--context IrbyDbContext \
--output-dir Migrations/20220303192042_AddFileandJobName
# Apply to database
dotnet ef database update
2. Querying Invoices by JobName
using (var db = new IrbyDbContext())
{
// Fetch all invoices for a given job
var invoices = db.BillingInvoice
.Where(b => b.JobName == "MySpecialJob")
.ToList();
}
β οΈ Error Handling & Return Types
- Migrations use the
voidreturn type forUp()andDown(). - Error Scenarios:
- If the column already exists β Migration will fail with SQL exception.
- If the schema/table is missing β Migration will fail at runtime.
- Rollbacks:
- Executing
dotnet ef database update <PreviousMigration>will callDown()and drop the column safely.
- Executing
π ThirdβParty Integrations
- Entity Framework Core: Provides the migration APIs.
- SQL Server (or any relational provider): Executes the
ALTER TABLEstatements under the hood.
π Conclusion
This EF Core migration (AddFileandJobName) is a lightweight schema update that introduces a JobName column to your irby.BillingInvoice table. By following the Up/Down pattern, it ensures that your database can be migrated forward or backward safely.
Happy Coding! π