π 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
- Overview & Purpose
- Dependencies & Technologies
- Key Functional Areas
- View Models & Data Models
- Helper Methods & Utilities
- Error Handling & Return Types
- API Endpoints Overview
- Third-party Integrations
- Sample Code Blocks
- Usage Examples
- Generated SQL
- UML Diagram
- 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
- Add
π οΈ 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
ApprovalDepartmentcolumn.
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
ApprovalDepartmentcolumn.
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.
- EF Core will throw exceptions (e.g.,
π 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
-
Add the Migration
dotnet ef migrations add ApprovalDepartment -
Apply to Database
dotnet ef database update -
Rollback (Down)
dotnet ef database update LastGoodMigration -
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
ApprovalDepartmentcolumn to theRequestOptiontable. - 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! π