๐Ÿ“„ Migration Documentation

File: IrbyImaging.DbContext/Migrations/20240318221107_UpdateLegalDashDBtables.cs
Namespace: IrbyImaging.DbContext.Migrations


๐Ÿ“‘ Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Entities Affected
  4. Key Functional Areas
    4.1 Up Method
    4.2 Down Method
  5. Execution Flow (Sequence Diagram)
  6. How to Run & Rollback
  7. Sample Code Snippets & Examples
  8. Conclusion

๐Ÿง Overview & Purpose

UpdateLegalDashDBtables is an Entity Framework Core Migration that ๐Ÿ› ๏ธ adds or ๐Ÿงน removes primary keys to a set of tables related to the โ€œLegal Dashboardโ€ feature in the IrbyImaging application.

  • Why?
    • Ensure each table has a defined primary key for data integrity and EF Core tracking.
    • Support new queries, relationships, and dashboard reporting features.

โš™๏ธ Dependencies & Technologies

Technology / Package Version (Example) Purpose
Microsoft.EntityFrameworkCore โ‰ฅ 6.0.0 Core EF infrastructure for migrations & DB access.
.NET SDK โ‰ฅ 6.0 Runtime & compiler environment.

No external thirdโ€‘party integrations are involved in this migration.


๐Ÿ“Š Entities Affected

Table Name Primary Key Column Action
StatePropertyCosts Id Add
LeasePropertyDetails Id Add
LeaseDetails DocumentImageId Add
DirectorProperties Id Add
ContractTypeNumber Id Add
ContractTypeDetails Id Add
ContractsByLead Id Add

๐Ÿš€ Key Functional Areas

4.1 Up Method

The Up method applies the migration:

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.AddPrimaryKey(
        name: "PK_StatePropertyCosts",
        table: "StatePropertyCosts",
        column: "Id");

    migrationBuilder.AddPrimaryKey(
        name: "PK_LeasePropertyDetails",
        table: "LeasePropertyDetails",
        column: "Id");

    migrationBuilder.AddPrimaryKey(
        name: "PK_LeaseDetails",
        table: "LeaseDetails",
        column: "DocumentImageId");

    migrationBuilder.AddPrimaryKey(
        name: "PK_DirectorProperties",
        table: "DirectorProperties",
        column: "Id");

    migrationBuilder.AddPrimaryKey(
        name: "PK_ContractTypeNumber",
        table: "ContractTypeNumber",
        column: "Id");

    migrationBuilder.AddPrimaryKey(
        name: "PK_ContractTypeDetails",
        table: "ContractTypeDetails",
        column: "Id");

    migrationBuilder.AddPrimaryKey(
        name: "PK_ContractsByLead",
        table: "ContractsByLead",
        column: "Id");
}
  • ๐Ÿ“Œ What it does:
    • Adds a PRIMARY KEY constraint on the specified column for each target table.
    • Names each constraint consistently (PK_{TableName}).

4.2 Down Method

The Down method reverts the migration:

protected override void Down(MigrationBuilder migrationBuilder)
{
    migrationBuilder.DropPrimaryKey(
        name: "PK_StatePropertyCosts",
        table: "StatePropertyCosts");

    migrationBuilder.DropPrimaryKey(
        name: "PK_LeasePropertyDetails",
        table: "LeasePropertyDetails");

    migrationBuilder.DropPrimaryKey(
        name: "PK_LeaseDetails",
        table: "LeaseDetails");

    migrationBuilder.DropPrimaryKey(
        name: "PK_DirectorProperties",
        table: "DirectorProperties");

    migrationBuilder.DropPrimaryKey(
        name: "PK_ContractTypeNumber",
        table: "ContractTypeNumber");

    migrationBuilder.DropPrimaryKey(
        name: "PK_ContractTypeDetails",
        table: "ContractTypeDetails");

    migrationBuilder.DropPrimaryKey(
        name: "PK_ContractsByLead",
        table: "ContractsByLead");
}
  • ๐Ÿ“Œ What it does:
    • Removes the PRIMARY KEY constraints added in Up(), restoring the previous state.

๐Ÿ”„ Execution Flow (Sequence Diagram)

sequenceDiagram
  participant Dev as Developer
  participant EF as EF Core Migration
  participant DB as Database

  Dev->>EF: dotnet ef database update
  EF->>DB: Begin Transaction
  DB-->>EF: Ready
  EF->>DB: Apply AddPrimaryKey(...) for each table
  DB-->>EF: Keys Added
  EF->>DB: Commit Transaction
  DB-->>EF: Confirm Commit
  EF-->>Dev: Migration Succeeded โœ…

๐Ÿ”จ How to Run & Rollback

Run Migration

# 1. Ensure your project references IrbyImaging.DbContext
# 2. In your shell:
dotnet ef database update 20240318221107_UpdateLegalDashDBtables

Rollback Migration

# Revert to the prior migration (e.g., 20240317210000_PreviousMigration)
dotnet ef database update 20240317210000_PreviousMigration

๐Ÿ’ก Sample Code Snippets & Examples

  1. Checking the Database State
    After running dotnet ef database update, open SQL Server Management Studio (SSMS) and verify:

    EXEC sp_helpconstraint 'StatePropertyCosts';
    -- Should list PK_StatePropertyCosts on column [Id]
    
  2. Querying via EF Core

    using var context = new IrbyImagingDbContext();
    var stateCost = context.StatePropertyCosts.Find(123);  
    Console.WriteLine($"ID: {stateCost.Id}, Cost: {stateCost.Cost}");
    

๐Ÿ Conclusion

This migration, 20240318221107_UpdateLegalDashDBtables, is designed to enforce data integrity by adding primary keys across several legalโ€‘dashboard tables. Implementing this change ensures:

  • โœ… Entity Tracking in EF Core
  • โœ… Performance Gains on lookups and joins
  • โœ… Clear Constraint Naming for easier maintenance

Use the provided commands and code snippets to integrate and verify these schema updates in your development or production environment.


Happy Coding! ๐Ÿš€
IrbyImaging DbContext Team