📋 Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Migration Class Structure
  4. Key Functional Areas
  5. Schema Changes Summary
  6. Usage & Examples
  7. UML Diagrams
  8. Best Practices & Considerations
  9. 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 bit column named IsDeleted
  • 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 IsDeleted column, 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

  1. Scaffold the migration (if not already):

    dotnet ef migrations add AddDeletedColumnJobClass \
      --context IrbyImagingDbContext \
      --output-dir Migrations
    
  2. Apply to your database:

    dotnet ef database update
    
  3. Entity Model Update (optional but recommended):

    public class JobClassification
    {
        public int Id { get; set; }
        // … other properties …
        public bool IsDeleted { get; set; } = false;
    }
    
  4. Global Query Filter (soft‑delete pattern):

    protected override void OnModelCreating(ModelBuilder builder)
    {
        builder.Entity<JobClassification>()
               .HasQueryFilter(j => !j.IsDeleted);
        // …
    }
    
  5. Soft‑Delete Operation:

    var job = context.JobClassifications.Find(id);
    job.IsDeleted = true;
    context.SaveChanges();
    
  6. 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: false prevents 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! 🚀