📑 Table of Contents
- Overview & Purpose
- Key Functional Areas
2.1. Up Method
2.2. Down Method - Dependencies & Technologies
- Data Models & Relationships
- Helper Methods & Utilities
- Error Handling & Return Types
- API Endpoints Overview
- Third‑party Integrations
- Sample Code Blocks
- Usage Examples
- UML Class Diagram
- Conclusion
1. Overview & Purpose
📌 File: IrbyImaging.DbContext/Migrations/20230706201721_DropForeignKeys.cs
📆 Created on: 2023-07-06 20:17:21
🔍 Migration Name: DropForeignKeys
This Entity Framework Core migration drops two foreign key constraints on the irby.TimeSheetEntry table in the Up method and re-creates them in the Down method.
Why?
- To remove or refactor existing relationships between
TimeSheetEntryand its parent tables (Employee&TimeEntryType). - To prepare for schema changes (e.g., renaming columns, splitting tables, or loading data in pre/post steps).
2. Key Functional Areas
2.1. Up Method
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_TimeSheetEntry_Employee_Id",
schema: "irby",
table: "TimeSheetEntry");
migrationBuilder.DropForeignKey(
name: "FK_TimeSheetEntry_TimeEntryType_Id",
schema: "irby",
table: "TimeSheetEntry");
}
- Action:
- Removes the foreign key from
TimeSheetEntry(Id) → Employee(Id). - Removes the foreign key from
TimeSheetEntry(Id) → TimeEntryType(Id).
- Removes the foreign key from
| FK Name | Principal Table | Child Table | Column | On Delete |
|---|---|---|---|---|
| FK_TimeSheetEntry_Employee_Id | Employee | TimeSheetEntry | Id | (n/a when dropped) |
| FK_TimeSheetEntry_TimeEntryType_Id | TimeEntryType | TimeSheetEntry | Id | (n/a when dropped) |
2.2. Down Method
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddForeignKey(
name: "FK_TimeSheetEntry_Employee_Id",
schema: "irby",
table: "TimeSheetEntry",
column: "Id",
principalSchema: "irby",
principalTable: "Employee",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
migrationBuilder.AddForeignKey(
name: "FK_TimeSheetEntry_TimeEntryType_Id",
schema: "irby",
table: "TimeSheetEntry",
column: "Id",
principalSchema: "irby",
principalTable: "TimeEntryType",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
}
- Action:
- Restores the dropped foreign keys, each with Cascade delete behavior.
| FK Name | Principal Table | Child Table | Column | On Delete |
|---|---|---|---|---|
| FK_TimeSheetEntry_Employee_Id | Employee | TimeSheetEntry | Id | Cascade |
| FK_TimeSheetEntry_TimeEntryType_Id | TimeEntryType | TimeSheetEntry | Id | Cascade |
3. Dependencies & Technologies
- .NET 6.0+ (or compatible)
- Entity Framework Core
- NuGet Package:
Microsoft.EntityFrameworkCore - Migrations API:
MigrationBuilder
- NuGet Package:
- C# 10.0 (nullable disabled)
- irby database schema
4. Data Models & Relationships
While this file only alters constraints, it touches on three EF Core entities:
| Entity | Schema | Keys | Notes |
|---|---|---|---|
| TimeSheetEntry | irby | Id (PK) | Logging time entries for employees |
| Employee | irby | Id (PK) | Master employee record |
| TimeEntryType | irby | Id (PK) | Reference types (e.g., Regular, Overtime, PTO) |
Original Relationships
TimeSheetEntry.Id→Employee.IdTimeSheetEntry.Id→TimeEntryType.Id
Post‑Migration (Up)
- The above relationships are removed.
5. Helper Methods & Utilities
❗ None in this migration file.
All operations rely on EF Core’s built‑in MigrationBuilder.
6. Error Handling & Return Types
- Error Handling: Delegated to EF Core's migration runner.
- Return Types: Both
UpandDownarevoid.
7. API Endpoints Overview
🚫 Not Applicable
This is a data-layer migration, no web/API layer here.
8. Third‑party Integrations
- Entity Framework Core only.
- No external SDKs or services involved.
9. Sample Code Blocks
// Dropping a foreign key in EF Core Migration
migrationBuilder.DropForeignKey(
name: "FK_MyTable_OtherTable_MyFk",
schema: "myschema",
table: "MyTable");
// Adding it back
migrationBuilder.AddForeignKey(
name: "FK_MyTable_OtherTable_MyFk",
schema: "myschema",
table: "MyTable",
column: "MyFk",
principalSchema: "myschema",
principalTable: "OtherTable",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
10. Usage Examples
- Add Migration
dotnet ef migrations add DropForeignKeys - Apply Migration
dotnet ef database update - Revert Migration
dotnet ef database update LastGoodMigration
11. UML Class Diagram
@startuml
class Migration {
+ void Up(MigrationBuilder)
+ void Down(MigrationBuilder)
}
class DropForeignKeys << (M,#FF7700) >> {
}
Migration <|-- DropForeignKeys
@enduml
12. Conclusion
This DropForeignKeys migration is a schema maintenance step in the IrbyImaging project. It allows you to safely remove and restore two critical relationships on the TimeSheetEntry table. Use this migration when refactoring or cleaning up your database schema before applying further changes.
Happy coding! 🎉