πŸ“‘ Table of Contents

  1. Overview & Purpose
  2. Context & Background
  3. Dependencies & Technologies
  4. Migration Class Structure
  5. Code Walkthrough
  6. How to Apply the Migration
  7. Rolling Back the Migration
  8. UML Diagram
  9. Usage Examples
  10. Conclusion

πŸ” Overview & Purpose

File: 20230620165643_AddIsDeletedTempDocumentImage.cs
Location: IrbyImaging.DbContext/Migrations/

This Entity Framework Core migration introduces a soft-delete capability for temporary document images in the database. By adding an IsDeleted flag to the TempDocumentData table, we can mark rows as β€œdeleted” without physically removing themβ€”enabling audit trails, undelete scenarios, and safer data handling.


πŸ—οΈ Context & Background

In many applications, it’s important to preserve historical or β€œdeleted” data rather than immediately purging it. A common pattern is soft deletion, where:

  • A boolean column (e.g., IsDeleted) indicates deletion status.
  • Queries filter out rows where IsDeleted = true by default.
  • Data can be β€œrestored” by toggling the flag back to false.

This migration follows a standard EF Core workflow:

  1. Developer adds a migration via CLI or Package Manager Console.
  2. EF scaffolds a new class under Migrations/.
  3. The developer reviews/adjusts the generated code.
  4. The migration is applied to the database, updating the schema.

βš™οΈ Dependencies & Technologies

Technology / Package Purpose
Microsoft.EntityFrameworkCore.Migrations Core EF migrations framework
.NET (C#) Host language for the DbContext project
SQL Server (assumed) Underlying RDBMS (bit β†’ Boolean type)

🧩 Migration Class Structure

namespace IrbyImaging.DbContext.Migrations
{
    public partial class AddIsDeletedTempDocumentImage : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder) { … }
        protected override void Down(MigrationBuilder migrationBuilder) { … }
    }
}
  • Namespace: IrbyImaging.DbContext.Migrations
  • Class: AddIsDeletedTempDocumentImage (inherits from Migration)
  • Methods:
    • Up(...) – defines how to apply changes.
    • Down(...) – defines how to revert changes.

πŸ” Code Walkthrough

Up Method (Apply Migration)

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.AddColumn<bool>(
        name: "IsDeleted",
        schema: "irby",
        table: "TempDocumentData",
        type: "bit",
        nullable: false,
        defaultValue: false);
}

What it does:

  • AddColumn<bool>: Adds a new column of type bit (SQL Server boolean)
  • name: "IsDeleted" – the soft-delete flag
  • schema: "irby" – database schema where the table resides
  • table: "TempDocumentData" – the target table
  • nullable: false – column must have a value
  • defaultValue: false – existing rows default to not deleted

Down Method (Revert Migration)

protected override void Down(MigrationBuilder migrationBuilder)
{
    migrationBuilder.DropColumn(
        name: "IsDeleted",
        schema: "irby",
        table: "TempDocumentData");
}

What it does:

  • DropColumn: Removes the IsDeleted column (rolls back the schema change)

πŸš€ How to Apply the Migration

  1. Ensure your project references Microsoft.EntityFrameworkCore.Design.

  2. Open a terminal (or Package Manager Console in Visual Studio).

  3. Run:

    CLI:

    dotnet ef database update
    

    PMC:

    Update-Database
    
  4. Verify the TempDocumentData table now contains the IsDeleted column.


↩️ Rolling Back the Migration

If you need to undo this change:

CLI:

dotnet ef database update LastGoodMigrationName

PMC:

Update-Database -Migration LastGoodMigrationName

This executes the Down method, dropping the IsDeleted column.


πŸ“Š UML Diagram

classDiagram
    class Migration {
        <<abstract>>
        +Up(MigrationBuilder)
        +Down(MigrationBuilder)
    }

    class AddIsDeletedTempDocumentImage {
        +Up(MigrationBuilder)
        +Down(MigrationBuilder)
    }

    Migration <|-- AddIsDeletedTempDocumentImage

🎯 Usage Examples

Querying Soft-Deleted Rows

// Standard query: exclude deleted
var activeTempDocs = dbContext.TempDocumentData
    .Where(d => !d.IsDeleted)
    .ToList();

// Include deleted
var allTempDocs = dbContext.TempDocumentData.ToList();

Marking a Record as Deleted

var doc = dbContext.TempDocumentData.Find(id);
if (doc != null)
{
    doc.IsDeleted = true;
    dbContext.SaveChanges();
}

Restoring a Soft-Deleted Record

var doc = dbContext.TempDocumentData
    .IgnoreQueryFilters()     // if using global filters
    .FirstOrDefault(d => d.Id == id && d.IsDeleted);
if (doc != null)
{
    doc.IsDeleted = false;
    dbContext.SaveChanges();
}

🏁 Conclusion

This migration adds essential soft-delete support to the TempDocumentData table by introducing an IsDeleted flag. Soft deletion enhances data safety, auditability, and flexibility for restore operations. By following the steps above, you can apply or revert this migration seamlessly within your EF Core workflow.

Happy coding! πŸš€