πŸ“„ Documentation: 20240131155030_ApprovalDepartment.cs

Path: IrbyImaging.DbContext/Migrations/20240131155030_ApprovalDepartment.cs
Type: Entity Framework Core Migration
Created On: 2024-01-31 15:50:30


πŸ“‘ Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Key Functional Areas
  4. View Models & Data Models
  5. Helper Methods & Utilities
  6. Error Handling & Return Types
  7. API Endpoints Overview
  8. Third-party Integrations
  9. Sample Code Blocks
  10. Usage Examples
  11. Generated SQL
  12. UML Diagram
  13. Conclusion

πŸ”Ž Overview & Purpose

This migration introduces a new column ApprovalDepartment to the RequestOption table in the irby schema.

  • Why? To store the department responsible for approving requests.
  • When? Applied as part of the database versioning workflow (e.g., dotnet ef database update).
  • What changes?
    • Add ApprovalDepartment (nvarchar(max), nullable)
    • Remove it upon rollback

πŸ› οΈ Dependencies & Technologies

Component Version / Notes
Microsoft.EntityFrameworkCore β‰₯ 7.0.0
Microsoft.EntityFrameworkCore.Migrations β‰₯ 7.0.0
.NET SDK β‰₯ .NET 7.0 (or compatible)
SQL Server (or any EF Core provider) Used for nvarchar(max) column type

πŸ”‘ Key Functional Areas

Up Method πŸ“ˆ

  • Purpose: Apply new changes to the database.
  • Action: Adds the ApprovalDepartment column.
protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.AddColumn<string>(
        name: "ApprovalDepartment",
        schema: "irby",
        table: "RequestOption",
        type: "nvarchar(max)",
        nullable: true);
}

Down Method πŸ“‰

  • Purpose: Revert the changes made in Up.
  • Action: Drops the ApprovalDepartment column.
protected override void Down(MigrationBuilder migrationBuilder)
{
    migrationBuilder.DropColumn(
        name: "ApprovalDepartment",
        schema: "irby",
        table: "RequestOption");
}

πŸ—οΈ View Models & Data Models

Category Details
View Models N/A – This is a pure database migration, no UI/view models are included.
Data Models RequestOption (existing table)
β€’ Schema: irby
β€’ New Column:   – ApprovalDepartment (string, nvarchar(max), nullable)

🧩 Helper Methods & Utilities

Method Description
migrationBuilder.AddColumn<> Adds a new column to an existing table.
migrationBuilder.DropColumn Removes an existing column from a table.

❗ Error Handling & Return Types

  • Return Type: void
  • Error Handling:
    • EF Core will throw exceptions (e.g., SqlException) if the column already exists or if SQL syntax errors occur.
    • Rollbacks occur automatically if an error is unhandled during Update-Database.

🌐 API Endpoints Overview

This file does not define any HTTP/API endpoints. It purely manages schema changes.


πŸ”Œ Third-party Integrations

  • Entity Framework Core – Core library handling Change Tracking, Migrations, and SQL generation.
  • SQL Server – Default provider (uses nvarchar(max)).

πŸ’» Sample Code Blocks

Full Migration Class

using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace IrbyImaging.DbContext.Migrations
{
    public partial class ApprovalDepartment : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<string>(
                name: "ApprovalDepartment",
                schema: "irby",
                table: "RequestOption",
                type: "nvarchar(max)",
                nullable: true);
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "ApprovalDepartment",
                schema: "irby",
                table: "RequestOption");
        }
    }
}

βš™οΈ Usage Examples

  1. Add the Migration

    dotnet ef migrations add ApprovalDepartment
    
  2. Apply to Database

    dotnet ef database update
    
  3. Rollback (Down)

    dotnet ef database update LastGoodMigration
    
  4. Programmatic Migration

    using(var context = new IrbyDbContext())
    {
        context.Database.Migrate();
    }
    

πŸ“ Generated SQL

ALTER TABLE [irby].[RequestOption]
ADD [ApprovalDepartment] nvarchar(max) NULL;

Discard SQL (Down):

ALTER TABLE [irby].[RequestOption]
DROP COLUMN [ApprovalDepartment];

πŸ“Š UML Diagram

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚      Migration          β”‚
β”‚Β«abstractΒ»               β”‚
β”‚ + Up(MigrationBuilder) │◀───┐
β”‚ + Down(MigrationBuilder)β”‚    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚
            β–²                  β”‚
            β”‚ ApprovalDepartment Migration
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ public partial class ApprovalDepartment  β”‚
β”‚   : Migration                            β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

🏁 Conclusion

  • What: Introduces the ApprovalDepartment column to the RequestOption table.
  • Why: Support tracking of which department approves a given request.
  • How: Via standard EF Core Migration (Up/Down).
  • Next Steps:
    • Update any Data Access Layer or Repositories to handle the new property.
    • Adjust Business Logic and UI (if applicable) to set/read ApprovalDepartment.

Happy coding! πŸš€