๐Ÿ—‚๏ธ IrbyImaging.DbContext Migration

20240318231947_UpdateLegalDashDBtables2.cs


๐Ÿ“‘ Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Migration Class Structure
  4. Up() Method: Applying Schema Changes
  5. Down() Method: Reverting Changes
  6. Detailed Rename Operations
  7. UML Class Diagram
  8. Usage Examples
  9. Impact Analysis & Best Practices
  10. Conclusion

1. ๐Ÿ“Œ Overview & Purpose

This Entity Framework Core Migration class moves a set of existing tables into the irby schema.

  • Why? To group legalโ€‘dashboard related tables under a dedicated schema (irby), improving organization, security, and maintainability.
  • When is it applied? During database version upgrades, typically via dotnet ef database update.

2. ๐Ÿ”ง Dependencies & Technologies

Component Version / Type
.NET / C# .NET 7+ / C# 10+
Entity Framework Core โ‰ฅ EF Core 6
Microsoft.EntityFrameworkCore.Migrations Migration API for schema management
Database Provider SQL Server / PostgreSQL / etc.

3. โš™๏ธ Migration Class Structure

Every EF Core Migration inherits from the Migration base class and overrides two main methods:

using Microsoft.EntityFrameworkCore.Migrations;

namespace IrbyImaging.DbContext.Migrations
{
    public partial class UpdateLegalDashDBtables2 : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder) { /* apply */ }
        protected override void Down(MigrationBuilder migrationBuilder) { /* revert */ }
    }
}
  • Up: Defines the operations to apply when migrating forward (e.g., renaming tables into a new schema).
  • Down: Defines the reverse operations to roll back the migration if needed.

4. ๐Ÿ”ผ Up() Method: Applying Schema Changes

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.RenameTable(
        name: "StatePropertyCosts",
        newName: "StatePropertyCosts",
        newSchema: "irby");

    migrationBuilder.RenameTable(
        name: "LeasePropertyDetails",
        newName: "LeasePropertyDetails",
        newSchema: "irby");

    // ... (other RenameTable calls)
}
  • RenameTable: Moves a table into a different schema without altering the table name.
  • name: The current table name (defaults to dbo schema).
  • newName: The name to which the table remains (no change here).
  • newSchema: The target schema (irby).

5. ๐Ÿ”ฝ Down() Method: Reverting Changes

protected override void Down(MigrationBuilder migrationBuilder)
{
    migrationBuilder.RenameTable(
        name: "StatePropertyCosts",
        schema: "irby",
        newName: "StatePropertyCosts");

    migrationBuilder.RenameTable(
        name: "LeasePropertyDetails",
        schema: "irby",
        newName: "LeasePropertyDetails");

    // ... (other RenameTable calls)
}
  • Reverses each RenameTable by moving tables out of the irby schema (back to default schema).

6. ๐Ÿ“‹ Detailed Rename Operations

Original Table New Schema Reversible?
StatePropertyCosts irby โœ”๏ธ
LeasePropertyDetails irby โœ”๏ธ
LeaseDetails irby โœ”๏ธ
DirectorProperties irby โœ”๏ธ
DirectorLeaseInfo irby โœ”๏ธ
ContractTypeNumber irby โœ”๏ธ
ContractTypeDetails irby โœ”๏ธ
ContractsByLead irby โœ”๏ธ

7. ๐Ÿ“Š UML Class Diagram

+--------------------------------------------+
|           UpdateLegalDashDBtables2        |
+--------------------------------------------+
| <<EF Core Migration>>                      |
+--------------------------------------------+
| + Up(migrationBuilder: MigrationBuilder)  |
| + Down(migrationBuilder: MigrationBuilder)|
+--------------------------------------------+
  • Class: UpdateLegalDashDBtables2
  • Base: Migration

8. ๐Ÿš€ Usage Examples

8.1. Generating a Migration

dotnet ef migrations add UpdateLegalDashDBtables2 \
    --project IrbyImaging.DbContext \
    --startup-project IrbyImaging.Api

8.2. Applying the Migration

dotnet ef database update \
    --project IrbyImaging.DbContext \
    --startup-project IrbyImaging.Api

8.3. Rolling Back

dotnet ef database update LastGoodMigrationName

9. ๐Ÿ” Impact Analysis & Best Practices

  • Zeroโ€‘Downtime Deployment

    • Renaming a table is a metadata operation; typically quick.
    • Ensure no active queries during rename to avoid locks.
  • Schema Qualification

    • After migrating, update your DbContext mappings (if explicit schema is used).
      modelBuilder.Entity<StatePropertyCost>()
                  .ToTable("StatePropertyCosts", "irby");
      
  • Testing

    • Validate in a staging environment.
    • Run database integration tests to confirm that table references still resolve correctly.

10. ๐Ÿ Conclusion

This migration centralizes legalโ€‘dashboard tables under the irby schema, enhancing modularity and clarity of your database structure. Following the commands and best practices above, you can apply or roll back safely while maintaining data integrity and application compatibility.


Authored with โค๏ธ for maintainable database evolution.