ποΈ RemoveDocImageFKToWOTable Migration Documentation
Filename:
IrbyImaging.DbContext/Migrations/20240301143253_RemoveDocImageFKToWOTable.cs
π Table of Contents
- Overview & Purpose
- Key Functional Areas
- Dependencies & Technologies
- Migration Flow UML Diagram
- Code Walkthrough
- Usage Examples
- Error Handling & Return Types
- API Endpoints & View Models
- Thirdβparty Integrations
- Conclusion
1. Overview & Purpose
π― Purpose
This Entity Framework Core (EF Core) migration removes the foreign key relationship and associated column DocumentImageId from the WorkOrder table. It ensures that the schema no longer requires or enforces a link between WorkOrder records and DocumentImage entities.
π§© Context
- Project: IrbyImaging
- Schema:
irby - Affected Tables:
WorkOrderDocumentImage
2. Key Functional Areas
| Area | Description |
|---|---|
| Up Method | Drops FK constraint, index, and column from WorkOrder. |
| Down Method | Reverses changes: re-adds column, index, and FK to restore original state. |
Up Method
- Drop Foreign Key
- Drop Index
- Drop Column
Down Method
- Add Column
- Create Index
- Add Foreign Key
3. Dependencies & Technologies
- Language: C#
- Platform: .NET 7+
- ORM: Entity Framework Core
- NuGet Packages:
Microsoft.EntityFrameworkCoreMicrosoft.EntityFrameworkCore.Relational
4. Migration Flow UML Diagram
ββββββββββββββββββββ βββββββββββββββββββββ
β RemoveDocImageFK β β MigrationBuilder β
β ToWOTable βββββββββββΆβ β
ββββββββββββββββββββ βββββββββββββββββββββ
β² βΌ
β β
Up() β β Down()
β β
βΌ β²
ββββββββββββββββββββββββββββββ
β WorkOrder (irby schema) β
ββββββββββββββββββββββββββββββ
5. Code Walkthrough
using System;
using Microsoft.EntityFrameworkCore.Migrations;
namespace IrbyImaging.DbContext.Migrations
{
public partial class RemoveDocImageFKToWOTable : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
// 1. Drop FK constraint
migrationBuilder.DropForeignKey(
name: "FK_WorkOrder_DocumentImage_DocumentImageId",
schema: "irby",
table: "WorkOrder");
// 2. Drop Index on DocumentImageId
migrationBuilder.DropIndex(
name: "IX_WorkOrder_DocumentImageId",
schema: "irby",
table: "WorkOrder");
// 3. Drop the column DocumentImageId
migrationBuilder.DropColumn(
name: "DocumentImageId",
schema: "irby",
table: "WorkOrder");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
// 1. Re-add the column
migrationBuilder.AddColumn<Guid>(
name: "DocumentImageId",
schema: "irby",
table: "WorkOrder",
type: "uniqueidentifier",
nullable: true);
// 2. Re-create the index
migrationBuilder.CreateIndex(
name: "IX_WorkOrder_DocumentImageId",
schema: "irby",
table: "WorkOrder",
column: "DocumentImageId");
// 3. Re-establish the FK relationship
migrationBuilder.AddForeignKey(
name: "FK_WorkOrder_DocumentImage_DocumentImageId",
schema: "irby",
table: "WorkOrder",
column: "DocumentImageId",
principalSchema: "irby",
principalTable: "DocumentImage",
principalColumn: "DocumentImageId");
}
}
}
5.1 Up Method Details
-
DropForeignKey
- Purpose: Remove referential integrity check linking
WorkOrder.DocumentImageIdβDocumentImage.DocumentImageId. - Effect: Prevents cascade-deletes or enforcement of relationship.
- Purpose: Remove referential integrity check linking
-
DropIndex
- Purpose: Remove the supporting index
IX_WorkOrder_DocumentImageId. - Effect: Improves performance of FK lookupsβmust be dropped first.
- Purpose: Remove the supporting index
-
DropColumn
- Purpose: Physically remove the
DocumentImageIdcolumn. - Effect:
WorkOrdertable no longer stores any reference toDocumentImage.
- Purpose: Physically remove the
5.2 Down Method Details
-
AddColumn<Guid>
- Re-introduce the
DocumentImageIdcolumn of typeuniqueidentifier, nullable.
- Re-introduce the
-
CreateIndex
- Recreate the non-unique index on the new column.
-
AddForeignKey
- Revert referential integrity: links back to
irby.DocumentImage.
- Revert referential integrity: links back to
6. Usage Examples
Applying the Migration
# 1. Add the migration to your project (already named):
dotnet ef migrations add RemoveDocImageFKToWOTable
# 2. Update the database:
dotnet ef database update
Rolling Back
# Rollback to prior migration
dotnet ef database update <PreviousMigrationName>
7. Error Handling & Return Types
- EF Core Exceptions:
SqlExceptionif the column/index/FK does not exist.InvalidOperationExceptionif attempted on a missing table/schema.
- Return Type:
BothUpandDownarevoid. Migrations are invoked by the EF runtime.
8. API Endpoints & View Models
π Not Applicable
This is strictly a database schema migrationβno HTTP endpoints or view models are defined.
9. Thirdβparty Integrations
- None
No external services or packages beyond EF Core are used.
10. Conclusion
β
RemoveDocImageFKToWOTable is a simple yet crucial migration to drop an outdated foreign key relationship between WorkOrder and DocumentImage.
- Use
Upto apply the change, andDownto revert. - Always backup your database and test migrations in a staging environment before production deployment.
Happy Coding! π