π IrbyImaging.DbContext Β· Migration: MoreUpdatesToJobTable
π Table of Contents
- Overview & Purpose
- Key Functional Areas
- Dependencies & Technologies
- Data Model: Job Table Before & After
- Migration Class Structure
- Up Method (Applying the Migration) π
- Down Method (Reverting the Migration) π
- UML Diagram π
- Usage Examples
- Conclusion
1. Overview & Purpose
This migration fileβ20240130134140_MoreUpdatesToJobTable.csβis part of the IrbyImaging.DbContext project and uses Entity Framework Core Migrations to alter the schema of the irby.Job table.
Primary goal:
- Add a new nullable
CustomerIdcolumn of typeuniqueidentifierto link each Job row optionally to a Customer. - Provide a rollback path by removing
CustomerIdin theDown()method.
βοΈ This change enables the application to associate imaging jobs with customers, supporting future features like customer-specific job queries.
2. Key Functional Areas
-
Schema Evolution
Leverage EF Core Migrations to update the database without manual SQL scripts. -
Column Management
- AddColumn in
Up() - DropColumn in
Down()
- AddColumn in
-
Version Control of Database
Track changes in code, maintain history, and enable rolling back if needed.
3. Dependencies & Technologies
| Type | Technology / Package |
|---|---|
| Programming Language | C# (.NET 7+) |
| ORM / Migrations Framework | Microsoft.EntityFrameworkCore |
| Database | SQL Server (assumed from uniqueidentifier) |
| Namespaces | System, Microsoft.EntityFrameworkCore.Migrations |
4. Data Model: Job Table Before & After
Before Migration
| Column Name | Data Type | Nullable | Description |
|---|---|---|---|
| Id | uniqueidentifier | False | Primary Key |
| Name | nvarchar(200) | False | Job name |
| CreatedDate | datetime2 | False | When the job was created |
| (other columns) | β¦ | β¦ | β¦ |
After Migration
| Column Name | Data Type | Nullable | Description |
|---|---|---|---|
| CustomerId | uniqueidentifier | True | Optional FK to Customer.Id |
5. Migration Class Structure
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace IrbyImaging.DbContext.Migrations
{
/// <inheritdoc />
public partial class MoreUpdatesToJobTable : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
// Add CustomerId column
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
// Remove CustomerId column
}
}
}
partial class MoreUpdatesToJobTable : Migration
Inherits from EF CoreβsMigrationbase class.Up()
Applies schema changes.Down()
Reverts schema changes.
6. Up Method (Applying the Migration) π
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<Guid>(
name: "CustomerId",
schema: "irby",
table: "Job",
type: "uniqueidentifier",
nullable: true);
}
What it does:
AddColumn<Guid>name: "CustomerId"β Column name.schema: "irby"β Database schema.table: "Job"β Target table.type: "uniqueidentifier"β SQL Server GUID type.nullable: trueβ Allows nulls (optional relationship).
π Result: The
Jobtable now has a CustomerId column, ready to store GUIDs.
7. Down Method (Reverting the Migration) π
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "CustomerId",
schema: "irby",
table: "Job");
}
What it does:
DropColumnβ Completely removesCustomerIdfromirby.Job.
β οΈ Caution: Dropping the column deletes all CustomerId data in existing rows.
8. UML Diagram π
+------------------------------------+
| << EFCore Migration >> |
| MoreUpdatesToJobTable |
+------------------------------------+
| + Up(MigrationBuilder) : void |
| + Down(MigrationBuilder) : void |
+------------------------------------+
β²
|
+---------------------------+
| Microsoft.EntityFrameworkCore.Migrations.Migration |
+---------------------------+
9. Usage Examples
9.1. Adding & Applying the Migration
# 1. Scaffold the migration
dotnet ef migrations add MoreUpdatesToJobTable \
--context IrbyDbContext \
--output-dir Migrations
# 2. Update the database
dotnet ef database update
9.2. Rolling Back (Reverting)
# Revert to the previous migration
dotnet ef database update <PreviousMigrationName>
9.3. Verifying the Change
-- In SQL Server Management Studio or Azure Data Studio
SELECT TOP 1 CustomerId FROM irby.Job;
10. Conclusion
This MoreUpdatesToJobTable migration is a focused, reversible database schema update that introduces a nullable CustomerId to the Job table. It paves the way for linking jobs to customers in your imaging workflow, all while maintaining a clear migration history. Applying and rolling back is straightforward via standard EF Core CLI commands, making your database evolution predictable and versioned.
Enjoy building customer-centric features! π
Generated by IrbyImaging.DbContext Migration Documentation Generator