πŸ“œ 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

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Key Functional Areas
  4. Detailed Breakdown
  5. Data Model Changes
  6. Usage & Examples
  7. UML Diagram
  8. 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 IsDeleted column to <schΓ©ma>.BillingInvoice.
  • Roll-back Support
    • Removes the IsDeleted column if the migration is rolled back.

πŸ”Ž 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 an IsDeleted flag.
  • 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

  1. Apply Migration

    dotnet ef migrations add AddIsDeleted
    dotnet ef database update
    
  2. Soft-Delete in Code

    // Mark as deleted
    var invoice = context.BillingInvoices.Find(id);
    invoice.IsDeleted = true;
    context.SaveChanges();
    
  3. 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.