πŸ—‚οΈ Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Migration Class Structure
  4. Schema Modification πŸ”§
  5. Code Implementation
  6. Usage Examples πŸš€
  7. UML Diagram πŸ—ΊοΈ
  8. Rollback Strategy πŸ”„
  9. Conclusion 🎯

1. Overview & Purpose

File:
IrbyImaging.DbContext/Migrations/20230629190543_AddNewColumnsBilling.cs

This Entity Framework Core Migration adds two new nullable string columnsβ€”CarrierName and EmployeeNameβ€”to the BillingInvoice table in the irby schema.

Why?

  • πŸ“¦ Enhance billing records with human‐readable references (carrier & employee).
  • πŸ” Facilitate reporting and auditing by capturing who processed and which carrier handled each invoice.

2. Dependencies & Technologies

Category Details
Framework .NET 6+, .NET Core
ORM Entity Framework Core (EF Core)
Migration Tooling Microsoft.EntityFrameworkCore.Migrations
Database (target) SQL Server (uses nvarchar(max))
Project Structure (DC) IrbyImaging.DbContext

3. Migration Class Structure

namespace IrbyImaging.DbContext.Migrations
{
    /// <inheritdoc />
    public partial class AddNewColumnsBilling : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            // Adds columns
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            // Drops columns
        }
    }
}

Key Methods

  • Up: Defines the schema changes to apply.
  • Down: Reverts those changes on rollback.

4. Schema Modification πŸ”§

New Columns in irby.BillingInvoice

Column Name SQL Type Nullable Description
CarrierName nvarchar(max) Yes Name of the carrier/service provider
EmployeeName nvarchar(max) Yes Name of the employee responsible for the invoice

5. Code Implementation

using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace IrbyImaging.DbContext.Migrations
{
    /// <inheritdoc />
    public partial class AddNewColumnsBilling : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<string>(
                name: "CarrierName",
                schema: "irby",
                table: "BillingInvoice",
                type: "nvarchar(max)",
                nullable: true);

            migrationBuilder.AddColumn<string>(
                name: "EmployeeName",
                schema: "irby",
                table: "BillingInvoice",
                type: "nvarchar(max)",
                nullable: true);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "CarrierName",
                schema: "irby",
                table: "BillingInvoice");

            migrationBuilder.DropColumn(
                name: "EmployeeName",
                schema: "irby",
                table: "BillingInvoice");
        }
    }
}

Highlights

  • AddColumn<string>: Adds a nullable string column.
  • DropColumn: Safely removes columns when rolling back.

6. Usage Examples πŸš€

1. Add Migration

dotnet ef migrations add AddNewColumnsBilling

This generates the above file with the timestamp prefix 20230629190543.

2. Apply to Database

dotnet ef database update

This will execute the Up method and update your schema.

3. Rollback Migration

dotnet ef database update LastGoodMigrationName

This rolls back by executing the Down method.


7. UML Diagram πŸ—ΊοΈ

+------------------------------------------------+
|        AddNewColumnsBilling (Migration)        |
+------------------------------------------------+
| + Up(migrationBuilder: MigrationBuilder)       |
| + Down(migrationBuilder: MigrationBuilder)     |
+------------------------------------------------+
  • Inheritance:
    AddNewColumnsBilling β­’ inherits from β­’ Microsoft.EntityFrameworkCore.Migrations.Migration

8. Rollback Strategy πŸ”„

  • Purpose: Ensures safe schema changes.

  • Procedure:

    1. Identify the last migration you want to retain (e.g., 20230629180000_PreviousMigration).
    2. Run:
      dotnet ef database update 20230629180000_PreviousMigration
      
    3. EF Core invokes Down in AddNewColumnsBilling to drop the newly added columns.

9. Conclusion 🎯

This migration is a simple yet crucial schema update that empowers the billing module to capture carrier and employee information. By following EF Core best practices for Up/Down methods and leveraging the .NET CLI, you can smoothly integrate, test, and roll back these changes in your development or production environments.

Happy coding! πŸ§‘β€πŸ’»πŸš€