๐๏ธ IrbyImaging.DbContext Migration
20240318231947_UpdateLegalDashDBtables2.cs
๐ Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Migration Class Structure
- Up() Method: Applying Schema Changes
- Down() Method: Reverting Changes
- Detailed Rename Operations
- UML Class Diagram
- Usage Examples
- Impact Analysis & Best Practices
- 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 todboschema).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
RenameTableby moving tables out of theirbyschema (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");
- After migrating, update your DbContext mappings (if explicit schema is used).
-
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.