π IrbyImaging.DbContext/Migrations/20240318162041_UpdateLegalDashboardTables.cs
π Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Key Functional Areas
- Migration Details
- Affected Tables & Schema Changes
- UML Diagram
- Applying & Rolling Back the Migration
- Sample Usage & Code Snippets
- 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-nullableIdcolumn of typeuniqueidentifier(GUID) to each target table. - Default Value
UsesGuid.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 addedIdcolumns, 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)
- Command Line
dotnet ef database update 20240318162041_UpdateLegalDashboardTables - Package Manager Console
Update-Database -Migration 20240318162041_UpdateLegalDashboardTables
Rolling Back (Down)
- Command Line
dotnet ef database update <PreviousMigrationName> - 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 π οΈ