πŸ“‘ Documentation: 20220209180647_AddDeletedColumn.cs

Module: IrbyImaging.DbContext/Migrations
Author: kvamshi
Created: 02-09-2022
Last Modified: 02-15-2022


πŸ“š Table of Contents

  1. Overview & Purpose
  2. Migration Structure
  3. Up() Method
  4. Down() Method
  5. UML Class Diagram
  6. Dependencies & Technologies
  7. How to Run This Migration
  8. Sample Code & Usage Examples
  9. Conclusion

πŸ” Overview & Purpose

This migration adds a soft-delete flag (IsDeleted) to the DocumentImage table within the irby schema.
Soft deletes are commonly used to mark a record as β€œdeleted” without physically removing it, enabling:

  • βœ… Data auditing
  • βœ… Historical analysis
  • βœ… Easy recovery

Key Change:

Aspect Before After
Column Name (none) IsDeleted
Data Type (n/a) bit
Default Value (n/a) false
Nullable (n/a) NOT NULL
Table irby.DocumentImage irby.DocumentImage (unchanged)

πŸ—οΈ Migration Structure

namespace IrbyImaging.DbContext.Migrations
{
    public partial class AddDeletedColumn : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            // Add new column
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            // Remove column
        }
    }
}
  • Namespace: IrbyImaging.DbContext.Migrations
  • Class: AddDeletedColumn inherits from Migration
  • Methods:
    • Up(...) β†’ apply schema changes
    • Down(...) β†’ rollback schema changes

⬆️ Up() Method

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

What happens here?

  1. AddColumn<bool>
    • Adds a new bool column to the table.
  2. Parameters:
    Parameter Value Description
    name "IsDeleted" Column name in the database.
    schema "irby" Database schema name.
    table "DocumentImage" Table to alter.
    type "bit" SQL Server data type for boolean.
    nullable false Disallow NULL values.
    defaultValue false Existing rows default to false.

⬇️ Down() Method

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

Rollback steps:

  • DropColumn removes the IsDeleted column from the target table, reversing the Up() operation.

πŸ“Š UML Class Diagram

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

πŸ”§ Dependencies & Technologies

  • Entity Framework Core (Microsoft.EntityFrameworkCore.Migrations)
  • .NET 5+ / .NET Core (depending on project target)
  • SQL Server (inferred by bit data type)
  • C# 8.0+

πŸš€ How to Run This Migration

  1. Add Migration
    dotnet ef migrations add AddDeletedColumn \
        --project IrbyImaging.DbContext \
        --startup-project IrbyImaging.WebApi
    
  2. Apply to Database
    dotnet ef database update \
        --project IrbyImaging.DbContext \
        --startup-project IrbyImaging.WebApi
    
  3. Verify
    SELECT TOP 5 IsDeleted
    FROM irby.DocumentImage;
    

πŸ“ Sample Code & Usage Examples

Checking Soft-Delete in Repository

public async Task<List<DocumentImage>> GetActiveImagesAsync()
{
    return await _context.DocumentImages
                         .Where(img => !img.IsDeleted)
                         .ToListAsync();
}

public async Task SoftDeleteImageAsync(int imageId)
{
    var img = await _context.DocumentImages.FindAsync(imageId);
    if (img != null)
    {
        img.IsDeleted = true;
        await _context.SaveChangesAsync();
    }
}

βœ… Conclusion

The AddDeletedColumn migration introduces a robust mechanism for soft deletion in the DocumentImage table. By marking records as deleted rather than physically removing them, you gain:

  • πŸ” Auditability
  • βš™οΈ Simplified business logic (e.g., hiding vs. deleting)
  • πŸ›‘οΈ Data safety (easy recovery)

Apply and rollback seamlessly using EF Core’s migration tooling.


Happy coding! πŸš€