πŸ“ IrbyImaging.DbContext/Migrations/20240318162041_UpdateLegalDashboardTables.cs


πŸ“‘ Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Key Functional Areas
  4. Migration Details
  5. Affected Tables & Schema Changes
  6. UML Diagram
  7. Applying & Rolling Back the Migration
  8. Sample Usage & Code Snippets
  9. Conclusion

🧐 Overview & Purpose

This Entity Framework Core migration, named UpdateLegalDashboardTables, introduces a new GUID primary key column (Id) to seven existing tables used in the Legal Dashboard feature of the IrbyImaging application.
By adding a surrogate Id column, the migration ensures each table has a unique identifierβ€”improving referential integrity, enabling easier data manipulation, and aligning with best practices for modern relational designs.


βš™οΈ Dependencies & Technologies

Technology Version / Details
.NET / C# .NET 6+
Entity Framework Core EF Core Migrations
SQL Server uniqueidentifier data type support
Visual Studio / CLI Tools dotnet ef migrations add / update-database

πŸ”‘ Key Functional Areas

  • Schema Evolution
    Safely evolves the database schema by adding new columns without harming existing data.
  • Data Integrity
    Introduces surrogate keys to support future relations, indexing, and performance improvements.
  • Migration Safety
    Includes a Down method to rollback changes if needed.

πŸ“‚ Migration Details

Up Method

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.AddColumn<Guid>(
        name: "Id",
        table: "StatePropertyCosts",
        type: "uniqueidentifier",
        nullable: false,
        defaultValue: Guid.Empty);

    // ... repeated for each table ...
    migrationBuilder.AddColumn<Guid>(
        name: "Id",
        table: "ContractsByLead",
        type: "uniqueidentifier",
        nullable: false,
        defaultValue: Guid.Empty);
}
  • Action
    Adds a non-nullable Id column of type uniqueidentifier (GUID) to each target table.
  • Default Value
    Uses Guid.Empty (00000000-0000-0000-0000-000000000000) so existing rows are backfilled with a deterministic value.

Down Method

protected override void Down(MigrationBuilder migrationBuilder)
{
    migrationBuilder.DropColumn(name: "Id", table: "StatePropertyCosts");
    // ... repeated for each table ...
    migrationBuilder.DropColumn(name: "Id", table: "ContractsByLead");
}
  • Action
    Drops the previously added Id columns, effectively rolling back the schema to its prior state.

πŸ—ƒοΈ Affected Tables & Schema Changes

Table Name Column Added Data Type Nullable Default Value
StatePropertyCosts Id uniqueidentifier No 00000000-0000-0000-0000-000000000000
LeasePropertyDetails Id uniqueidentifier No 00000000-0000-0000-0000-000000000000
DirectorProperties Id uniqueidentifier No 00000000-0000-0000-0000-000000000000
DirectorLeaseInfo Id uniqueidentifier No 00000000-0000-0000-0000-000000000000
ContractTypeNumber Id uniqueidentifier No 00000000-0000-0000-0000-000000000000
ContractTypeDetails Id uniqueidentifier No 00000000-0000-0000-0000-000000000000
ContractsByLead Id uniqueidentifier No 00000000-0000-0000-0000-000000000000

πŸ“ˆ UML Diagram

Below is a simplified class-diagram representation of one of the modified tables. All seven tables follow the same pattern.

@startuml
entity "StatePropertyCosts" {
  * Guid Id
  --
  existingColumn1 : <type>
  existingColumn2 : <type>
  ... 
}
@enduml

πŸš€ Applying & Rolling Back the Migration

Applying (Up)

  1. Command Line
    dotnet ef database update 20240318162041_UpdateLegalDashboardTables
    
  2. Package Manager Console
    Update-Database -Migration 20240318162041_UpdateLegalDashboardTables
    

Rolling Back (Down)

  1. Command Line
    dotnet ef database update <PreviousMigrationName>
    
  2. Package Manager Console
    Update-Database -Migration <PreviousMigrationName>
    

πŸ“‹ Sample Usage & Code Snippets

Before (no Id column):

SELECT * FROM LeasePropertyDetails;
-- Columns: LeaseId, PropertyName, LeaseStart, LeaseEnd, ...

After (with surrogate key):

SELECT Id, LeaseId, PropertyName, LeaseStart, LeaseEnd
FROM LeasePropertyDetails;

Adding a new entry in code:

var newDetail = new LeasePropertyDetail {
    Id = Guid.NewGuid(),
    LeaseId = "...",
    PropertyName = "Downtown Office",
    LeaseStart = DateTime.UtcNow,
    LeaseEnd = DateTime.UtcNow.AddYears(1)
};
dbContext.LeasePropertyDetails.Add(newDetail);
await dbContext.SaveChangesAsync();

🎯 Conclusion

This migration is a crucial step towards enhancing the Legal Dashboard data model by introducing a consistent surrogate key (Id) across multiple summary tables.

  • βœ… Improves data integrity
  • βœ… Facilitates future relationships and indexing
  • βœ… Aligns with EF Core best practices

Be sure to re-deploy your application after applying this migration and update any existing code or queries that rely on these tables to leverage the new Id columns.


Generated by IrbyImaging DbContext Migration Analyzer πŸ› οΈ