π Documentation: 20220209180647_AddDeletedColumn.cs
Module: IrbyImaging.DbContext/Migrations
Author: kvamshi
Created: 02-09-2022
Last Modified: 02-15-2022
π Table of Contents
- Overview & Purpose
- Migration Structure
Up()MethodDown()Method- UML Class Diagram
- Dependencies & Technologies
- How to Run This Migration
- Sample Code & Usage Examples
- 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:
AddDeletedColumninherits fromMigration - Methods:
Up(...)β apply schema changesDown(...)β 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?
AddColumn<bool>- Adds a new
boolcolumn to the table.
- Adds a new
- 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. nullablefalseDisallow NULLvalues.defaultValuefalseExisting rows default to false.
β¬οΈ Down() Method
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "IsDeleted",
schema: "irby",
table: "DocumentImage"
);
}
Rollback steps:
DropColumnremoves theIsDeletedcolumn from the target table, reversing theUp()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
bitdata type) - C# 8.0+
π How to Run This Migration
- Add Migration
dotnet ef migrations add AddDeletedColumn \ --project IrbyImaging.DbContext \ --startup-project IrbyImaging.WebApi - Apply to Database
dotnet ef database update \ --project IrbyImaging.DbContext \ --startup-project IrbyImaging.WebApi - 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! π