π Table of Contents
- Overview & Purpose
- Context & Background
- Dependencies & Technologies
- Migration Class Structure
- Code Walkthrough
- How to Apply the Migration
- Rolling Back the Migration
- UML Diagram
- Usage Examples
- 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 = trueby default. - Data can be βrestoredβ by toggling the flag back to
false.
This migration follows a standard EF Core workflow:
- Developer adds a migration via CLI or Package Manager Console.
- EF scaffolds a new class under
Migrations/. - The developer reviews/adjusts the generated code.
- 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 fromMigration) - 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
IsDeletedcolumn (rolls back the schema change)
π How to Apply the Migration
-
Ensure your project references
Microsoft.EntityFrameworkCore.Design. -
Open a terminal (or Package Manager Console in Visual Studio).
-
Run:
CLI:
dotnet ef database updatePMC:
Update-Database -
Verify the
TempDocumentDatatable now contains theIsDeletedcolumn.
β©οΈ 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! π