📋 Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Migration Class Structure
- Key Functional Areas
- Schema Changes Summary
- Usage & Examples
- UML Diagrams
- Best Practices & Considerations
- Conclusion
📝 Overview & Purpose
The AddDeletedColumnJobClass migration introduces a new boolean column IsDeleted to the irby.JobClassification table. This change supports a soft-delete mechanism, enabling records to be marked as deleted without physically removing them from the database.
Why soft-delete?
- Keeps historical data for audit purposes
- Avoids accidental data loss
- Simplifies “undelete” scenarios
⚙️ Dependencies & Technologies
| Technology | Version / Details |
|---|---|
| .NET SDK | 6.0+ |
| Entity Framework | EF Core (Microsoft.EntityFrameworkCore) |
| Database Provider | SQL Server (type: "bit") |
| Schema | irby |
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
🏗️ Migration Class Structure
namespace IrbyImaging.DbContext.Migrations
{
public partial class AddDeletedColumnJobClass : Migration
{
protected override void Up(MigrationBuilder migrationBuilder) { /* … */ }
protected override void Down(MigrationBuilder migrationBuilder) { /* … */ }
}
}
- Class Name:
AddDeletedColumnJobClass - Base Class:
Migration - Partial: Allows EF-generated scaffold + manual tweaks
🔑 Key Functional Areas
Up Method (Adding IsDeleted Flag)
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<bool>(
name: "IsDeleted",
schema: "irby",
table: "JobClassification",
type: "bit",
nullable: false,
defaultValue: false);
}
- Adds a non-nullable
bitcolumn namedIsDeleted - Default Value:
false→ existing rows not considered “deleted”
Down Method (Removing IsDeleted Flag)
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "IsDeleted",
schema: "irby",
table: "JobClassification");
}
- Drops the
IsDeletedcolumn, rolling back the schema change
🗄️ Schema Changes Summary
| Column Name | Data Type | Nullable? | Default Value | Schema | Table |
|---|---|---|---|---|---|
| IsDeleted | bit |
No | false |
irby | JobClassification |
🚀 Usage & Examples
-
Scaffold the migration (if not already):
dotnet ef migrations add AddDeletedColumnJobClass \ --context IrbyImagingDbContext \ --output-dir Migrations -
Apply to your database:
dotnet ef database update -
Entity Model Update (optional but recommended):
public class JobClassification { public int Id { get; set; } // … other properties … public bool IsDeleted { get; set; } = false; } -
Global Query Filter (soft‑delete pattern):
protected override void OnModelCreating(ModelBuilder builder) { builder.Entity<JobClassification>() .HasQueryFilter(j => !j.IsDeleted); // … } -
Soft‑Delete Operation:
var job = context.JobClassifications.Find(id); job.IsDeleted = true; context.SaveChanges(); -
Undo Migration:
dotnet ef database update [PreviousMigrationName]
📈 UML Diagrams
1. Migration Class
classDiagram
class AddDeletedColumnJobClass {
<<Migration>>
+Up(MigrationBuilder)
+Down(MigrationBuilder)
}
2. Simplified Entity Relationship
classDiagram
class JobClassification {
+int Id
+string Name
+bool IsDeleted
}
🔍 Best Practices & Considerations
- Backup Data: Always back up before altering production schemas.
- Default Values: Setting
defaultValue: falseprevents null exceptions. - Update Model: Reflect column in your entity class for EF Core tracking.
- Query Filters: Implement global filters to automatically exclude “deleted” rows.
- Indexing: If you filter heavily on
IsDeleted, consider adding an index.
🏁 Conclusion
This migration adds a soft-delete flag to the JobClassification table, enhancing data integrity and auditability. By combining schema changes with EF Core’s query filters and application logic, you gain a robust method to mark records as deleted without permanent removal.
Feel free to extend upon this pattern across other entities to achieve a consistent soft-delete strategy throughout your domain! 🚀