π 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
- Overview & Purpose
- Dependencies & Technologies
- Migration Class Structure
3.1. Class Definition
3.2. Up() Method
3.3. Down() Method - Code Walkthrough
- UML Diagram
- Usage Examples
- Additional Notes & Best Practices
- 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
DbContextis configured to recognize theirbyschema, either viaOnModelCreatingor 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:
Migrationfrom 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()withschemaparameter pointing to"irby".
π Code Walkthrough
| Operation | Old Location | New Location |
|---|---|---|
| EquipmentType Table | Default Schema (dbo) | irby.EquipmentType |
| EquipmentClass Table | Default Schema (dbo) | irby.EquipmentClass |
- RenameTable
- Internally issues a SQL command like:
ALTER SCHEMA irby TRANSFER dbo.EquipmentType;
- Internally issues a SQL command like:
- Idempotence
- EF Core tracks the migration, ensuring it's only applied once.
- Reversibility
- The
Downmethod cleanly moves tables back to the original schema.
- The
π§© 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
- Add the Migration
From your project root:dotnet ef migrations add AddNewTablesForEquipmentReference2 - Apply to Database
dotnet ef database update - Rollback Migration
dotnet ef database update <PreviousMigrationName>
βοΈ Tip: Always review your generated SQL scripts (
--scriptflag) before applying to production.
π Additional Notes & Best Practices
- Default Schema
If you frequently work with custom schemas, configure yourDbContext:protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.HasDefaultSchema("irby"); // ... } - Permissions
Ensure the database user has rights toALTER 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! π