πŸ“š Documentation: 20240307193255_AddNewTablesForEquipmentReference2.cs


Project: IrbyImaging.DbContext
Folder: Migrations
Filename: 20240307193255_AddNewTablesForEquipmentReference2.cs
Migration Class: AddNewTablesForEquipmentReference2
Purpose: Move existing equipment reference tables into the irby database schema.


πŸ“– Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Migration Class Structure
    3.1. Class Definition
    3.2. Up() Method
    3.3. Down() Method
  4. Code Walkthrough
  5. UML Diagram
  6. Usage Examples
  7. Additional Notes & Best Practices
  8. Conclusion

πŸ“Œ Overview & Purpose

This Entity Framework Core migration is designed to reorganize two existing tablesβ€”EquipmentType and EquipmentClassβ€”into a dedicated schema named irby. By placing these reference tables under their own schema, we improve:

  • Data organization: Separating core lookup tables from transactional data.
  • Security: Applying schema-specific permissions.
  • Maintainability: Clearer project structure, especially as the application grows.

πŸ› οΈ Dependencies & Technologies

Technology / Package Version Purpose
Microsoft.EntityFrameworkCore 6.x or 7.x Core ORM for migrations, model building, etc.
.NET SDK 6.0+ or 7.0 Runtime and tooling

πŸ”§ Note: Ensure that your DbContext is configured to recognize the irby schema, either via OnModelCreating or data annotations.


πŸ—οΈ Migration Class Structure

3.1. Class Definition

using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace IrbyImaging.DbContext.Migrations
{
    /// <inheritdoc />
    public partial class AddNewTablesForEquipmentReference2 : Migration
    {
        // Up() & Down() implementation below...
    }
}
  • Namespace: IrbyImaging.DbContext.Migrations
  • Base Class: Migration from EF Core
  • Class Name: AddNewTablesForEquipmentReference2
  • Partial: Allows EF Core tools to scaffold additional code if needed.

3.2. Up() Method

protected override void Up(MigrationBuilder migrationBuilder)
{
    // Move EquipmentType into "irby" schema
    migrationBuilder.RenameTable(
        name: "EquipmentType",
        newName: "EquipmentType",
        newSchema: "irby");

    // Move EquipmentClass into "irby" schema
    migrationBuilder.RenameTable(
        name: "EquipmentClass",
        newName: "EquipmentClass",
        newSchema: "irby");
}
  • Purpose: Executes when applying the migration.
  • Key Operation: migrationBuilder.RenameTable()
    • name: Current table name (assumed in default schema).
    • newName: Target table name (same name).
    • newSchema: Destination schema ("irby").

3.3. Down() Method

protected override void Down(MigrationBuilder migrationBuilder)
{
    // Revert EquipmentType back to default schema
    migrationBuilder.RenameTable(
        name: "EquipmentType",
        schema: "irby",
        newName: "EquipmentType");

    // Revert EquipmentClass back to default schema
    migrationBuilder.RenameTable(
        name: "EquipmentClass",
        schema: "irby",
        newName: "EquipmentClass");
}
  • Purpose: Rolls back the schema migration when you dotnet ef database update <previous-migration>.
  • Key Operation: RenameTable() with schema parameter pointing to "irby".

πŸ” Code Walkthrough

Operation Old Location New Location
EquipmentType Table Default Schema (dbo) irby.EquipmentType
EquipmentClass Table Default Schema (dbo) irby.EquipmentClass
  1. RenameTable
    • Internally issues a SQL command like:
      ALTER SCHEMA irby TRANSFER dbo.EquipmentType;
      
  2. Idempotence
    • EF Core tracks the migration, ensuring it's only applied once.
  3. Reversibility
    • The Down method cleanly moves tables back to the original schema.

🧩 UML Diagram

classDiagram
    class Migration {
        <<abstract>>
        +Up(MigrationBuilder)
        +Down(MigrationBuilder)
    }
    class AddNewTablesForEquipmentReference2 {
        +Up(MigrationBuilder)
        +Down(MigrationBuilder)
    }
    Migration <|-- AddNewTablesForEquipmentReference2
    class MigrationBuilder {
        +RenameTable(name, newName, newSchema)
        +RenameTable(name, schema, newName)
    }
    AddNewTablesForEquipmentReference2 ..> MigrationBuilder : uses

πŸ’‘ Usage Examples

  1. Add the Migration
    From your project root:
    dotnet ef migrations add AddNewTablesForEquipmentReference2
    
  2. Apply to Database
    dotnet ef database update
    
  3. Rollback Migration
    dotnet ef database update <PreviousMigrationName>
    

βœ”οΈ Tip: Always review your generated SQL scripts (--script flag) before applying to production.


πŸ“ Additional Notes & Best Practices

  • Default Schema
    If you frequently work with custom schemas, configure your DbContext:
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultSchema("irby");
        // ...
    }
    
  • Permissions
    Ensure the database user has rights to ALTER SCHEMA.
  • Data Integrity
    Renaming tables does not affect dataβ€”only the schema path.
  • Backup
    Always take a backup before running schema migrations on production.
  • Consistency
    Keep naming conventions consistent across all tables and schemas.

🎯 Conclusion

This migration class, AddNewTablesForEquipmentReference2, provides a simple yet crucial refactoring to move two core lookup tables into a dedicated irby schema. By following the examples and best practices outlined above, you can maintain a clean, organized database structure and ensure smooth deployments.


Happy Coding! πŸš€