📂 IrbyImaging.DbContext Migrations
20240307125652_ChangeJobIdToNullable
Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Key Functional Areas
- EF Core Migration Structure
- Schema Before & After Change
- UML Diagram
- Usage Examples
- Conclusion
Overview & Purpose
File: IrbyImaging.DbContext/Migrations/20240307125652_ChangeJobIdToNullable.cs
This EF Core migration adjusts the Equipment table's JobId foreign‑key column to be nullable, allowing records of equipment that are not yet assigned to a job.
- ✏️ Why?
- In the original schema, every piece of equipment must reference a Job (non-nullable).
- Business scenarios emerged where equipment can exist without initially being associated with a job (e.g., newly procured or under maintenance).
Dependencies & Technologies
| Category | Details |
|---|---|
| Framework | .NET 7.0 / .NET Core |
| ORM | Entity Framework Core (Microsoft.EntityFrameworkCore.Migrations) |
| Database | SQL Server (uniqueidentifier type) |
| Schema | irby |
Key Functional Areas
- 🚧 Dropping Existing FK
- ✨ Altering Column Nullability
- 🔌 Re‑adding Foreign Key Constraint
- ↩️ Reversible Migration (via
Downmethod)
EF Core Migration Structure
All migrations inherit from Migration and define two methods:
- Up: Applies schema changes.
- Down: Reverts changes back to the previous state.
public partial class ChangeJobIdToNullable : Migration
{
protected override void Up(MigrationBuilder migrationBuilder) { /* … */ }
protected override void Down(MigrationBuilder migrationBuilder) { /* … */ }
}
Up Method
protected override void Up(MigrationBuilder migrationBuilder)
{
// 1️⃣ Drop existing foreign key
migrationBuilder.DropForeignKey(
name: "FK_Equipment_Job_JobId",
schema: "irby",
table: "Equipment");
// 2️⃣ Alter the JobId column to nullable
migrationBuilder.AlterColumn<Guid>(
name: "JobId",
schema: "irby",
table: "Equipment",
type: "uniqueidentifier",
nullable: true, // ← Change here
oldClrType: typeof(Guid),
oldType: "uniqueidentifier");
// 3️⃣ Re-add foreign key without cascade delete
migrationBuilder.AddForeignKey(
name: "FK_Equipment_Job_JobId",
schema: "irby",
table: "Equipment",
column: "JobId",
principalSchema: "irby",
principalTable: "Job",
principalColumn: "JobId");
}
Highlights
nullable: truemakes the column acceptNULL.- The FK is re‑added without a cascade delete (
onDelete: ReferentialAction.NoActionby default).
Down Method
protected override void Down(MigrationBuilder migrationBuilder)
{
// 1️⃣ Drop newly added foreign key
migrationBuilder.DropForeignKey(
name: "FK_Equipment_Job_JobId",
schema: "irby",
table: "Equipment");
// 2️⃣ Revert the JobId column to non-nullable, defaulting to empty GUID
migrationBuilder.AlterColumn<Guid>(
name: "JobId",
schema: "irby",
table: "Equipment",
type: "uniqueidentifier",
nullable: false, // ← Back to required
defaultValue: new Guid("00000000-0000-0000-0000-000000000000"),
oldClrType: typeof(Guid),
oldType: "uniqueidentifier",
oldNullable: true);
// 3️⃣ Re-add foreign key with cascade delete
migrationBuilder.AddForeignKey(
name: "FK_Equipment_Job_JobId",
schema: "irby",
table: "Equipment",
column: "JobId",
principalSchema: "irby",
principalTable: "Job",
principalColumn: "JobId",
onDelete: ReferentialAction.Cascade);
}
Highlights
- Resets
JobIdto non-nullable with a default value of00000000-0000-0000-0000-000000000000. - Re‑introduces cascade delete: deleting a Job will remove related Equipment.
Schema Before & After Change
| Aspect | Before | After |
|---|---|---|
| Column Nullability | JobId UNIQUEIDENTIFIER NOT NULL |
JobId UNIQUEIDENTIFIER NULL |
| FK Constraint | ON DELETE CASCADE |
ON DELETE NO ACTION (no cascade) |
| Default Value | N/A | N/A (defaults to NULL) |
UML Diagram
+-----------------------------------------+
| ChangeJobIdToNullable |
+-----------------------------------------+
| <<inherits>> Migration |
+-----------------------------------------+
| + Up(migrationBuilder: MigrationBuilder)|
| + Down(migrationBuilder: MigrationBuilder)|
+-----------------------------------------+
Usage Examples
-
Add Migration
dotnet ef migrations add ChangeJobIdToNullable -
Apply Migration
dotnet ef database update -
Revert (if needed)
dotnet ef database update 20240307125651_PreviousMigration
Conclusion
This migration offers a seamless transition to allow Equipment records to exist without a mandatory Job association, improving flexibility in handling newly procured or unassigned equipment. The reversible design ensures you can roll back safely if requirements change. 🎉
Generated by the IrbyImaging documentation generator.