๐ Migration Documentation
File: IrbyImaging.DbContext/Migrations/20240318221107_UpdateLegalDashDBtables.cs
Namespace: IrbyImaging.DbContext.Migrations
๐ Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Entities Affected
- Key Functional Areas
4.1 Up Method
4.2 Down Method - Execution Flow (Sequence Diagram)
- How to Run & Rollback
- Sample Code Snippets & Examples
- 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.
- Removes the PRIMARY KEY constraints added in
๐ 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
-
Checking the Database State
After runningdotnet ef database update, open SQL Server Management Studio (SSMS) and verify:EXEC sp_helpconstraint 'StatePropertyCosts'; -- Should list PK_StatePropertyCosts on column [Id] -
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