π IrbyImaging.DbContext.Migrations: 20220303202752_AddIsDeleted.cs
Welcome to the detailed documentation for the AddIsDeleted EF Core migration in the IrbyImaging.DbContext project. This guide will walk you through its purpose, structure, usage, and the changes it applies to your database schema.
π Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Key Functional Areas
- Detailed Breakdown
- Data Model Changes
- Usage & Examples
- UML Diagram
- Conclusion
π Overview & Purpose
This migration introduces a soft-delete mechanism to the BillingInvoice table by adding a new boolean column named IsDeleted. When set to true, records are considered βdeletedβ at the application level without physically removing them from the database. Soft-deletion helps:
- π‘ Audit/history tracking
- βοΈ Data recovery
- π Maintaining referential integrity
π§ Dependencies & Technologies
| Technology | Version / Role |
|---|---|
| .NET / C# | Language & runtime |
| EF Core (Microsoft.EntityFrameworkCore.Migrations) | Migrations infrastructure |
| SQL Server (bit type) | Underlying RDBMS column type |
Schema: irby |
Logical grouping in database |
π Key Functional Areas
- Schema Modification
- Adds a new
IsDeletedcolumn to<schΓ©ma>.BillingInvoice.
- Adds a new
- Roll-back Support
- Removes the
IsDeletedcolumn if the migration is rolled back.
- Removes the
π Detailed Breakdown
Full Source Code
// ***********************************************************************
// Assembly : IrbyImaging.DbContext
// Author : kvamshi
// Created : 03-31-2022
// Last Modified By : kvamshi
// Last Modified On : 03-31-2022
// ***********************************************************************
// <copyright file="20220303202752_AddIsDeleted.cs"
// company="IrbyImaging.DbContext">
// Copyright (c) . All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using Microsoft.EntityFrameworkCore.Migrations;
namespace IrbyImaging.DbContext.Migrations
{
/// <summary>
/// Class AddIsDeleted. Implements the <see cref="Migration" />
/// </summary>
public partial class AddIsDeleted : Migration
{
/// <summary>
/// Builds the operations that will migrate the database 'up'.
/// </summary>
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<bool>(
name: "IsDeleted",
schema: "irby",
table: "BillingInvoice",
type: "bit",
nullable: false,
defaultValue: false);
}
/// <summary>
/// Reverts the operations performed in Up().
/// </summary>
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "IsDeleted",
schema: "irby",
table: "BillingInvoice");
}
}
}
π Up() Method
| Parameter | Description |
|---|---|
migrationBuilder |
EF Coreβs builder for defining migration operations. |
What it does:
- Calls
AddColumn<bool>to add anIsDeletedflag. - Schema:
irby - Table:
BillingInvoice - SQL Type:
bit - Nullable:
false - Default Value:
false
migrationBuilder.AddColumn<bool>(
name: "IsDeleted",
schema: "irby",
table: "BillingInvoice",
type: "bit",
nullable: false,
defaultValue: false);
π» Down() Method
Reverts the operation performed in Up(). If you roll back this migration, EF Core will drop the IsDeleted column:
migrationBuilder.DropColumn(
name: "IsDeleted",
schema: "irby",
table: "BillingInvoice");
π Data Model Changes
After applying this migration, your BillingInvoice entity (C#) should be updated to include:
public class BillingInvoice
{
public int Id { get; set; }
// β¦ other properties β¦
/// <summary>
/// Indicates whether the record is soft-deleted.
/// </summary>
public bool IsDeleted { get; set; }
}
SQL Table Preview:
| Column | Type | Nullable | Default |
|---|---|---|---|
| Id | int | NO | β |
| β¦ | β¦ | β¦ | β¦ |
| IsDeleted | bit | NO | false |
π Usage & Examples
-
Apply Migration
dotnet ef migrations add AddIsDeleted dotnet ef database update -
Soft-Delete in Code
// Mark as deleted var invoice = context.BillingInvoices.Find(id); invoice.IsDeleted = true; context.SaveChanges(); -
Filter Out Deleted Records
var activeInvoices = context.BillingInvoices .Where(i => !i.IsDeleted) .ToList();
π UML Diagram
+-------------------------------------+
| <<Migration>> |
| AddIsDeleted |
+-------------------------------------+
| - Up(migrationBuilder) |
| - Down(migrationBuilder) |
+-------------------------------------+
π Conclusion
The AddIsDeleted migration is a small but powerful change that enables soft-deletion on the BillingInvoice table. By adding an IsDeleted flag rather than physically removing records, you gain:
- π Audit trails
- πΎ Data recovery capabilities
- π Referential integrity preservation
Remember to update your DbContext queries to filter out IsDeleted = true entries where appropriate. Happy coding! π
Generated on 03-31-2022 by kvamshi for the IrbyImaging.DbContext project.