20250211130437_AddColumnToPayrollMatchTable.cs
# π AddColumnToPayrollMatchTable Migration Documentation
> **File Path:** `IrbyImaging.DbContext/Migrations/20250211130437_AddColumnToPayrollMatchTable.cs`
> **Namespace:** `IrbyImaging.DbContext.Migrations`
---
## π Table of Contents
1. [Overview & Purpose](#1-overview--purpose)
2. [Dependencies & Technologies](#2-dependencies--technologies)
3. [Key Functional Areas](#3-key-functional-areas)
- [3.1 Up Method](#31-up-method)
- [3.2 Down Method](#32-down-method)
4. [Code Structure & UML Diagrams](#4-code-structure--uml-diagrams)
5. [View Models & Data Models](#5-view-models--data-models)
6. [Helper Methods & Utilities](#6-helper-methods--utilities)
7. [Error Handling & Rollbacks](#7-error-handling--rollbacks)
8. [API Endpoints Overview](#8-api-endpoints-overview)
9. [Thirdβparty Integrations](#9-third-party-integrations)
10. [Sample Code Blocks & CLI Usage](#10-sample-code-blocks--cli-usage)
11. [Usage Examples](#11-usage-examples)
12. [Conclusion](#12-conclusion)
---
## 1. Overview & Purpose π
This **Entity Framework Core Migration** adds a new column called `EmployeeNumber` to the `PayrollMatch` table in the database schema `irby`.
- **Why?**
- Expand the payroll reconciliation feature to track employees by a unique `EmployeeNumber`.
- Support downstream reporting and data integrity checks.
---
## 2. Dependencies & Technologies βοΈ
| Technology | Version / Package |
|---------------------------------|-------------------------------------------------------|
| .NET / C# | β₯ .NET 6.0 |
| Microsoft.EntityFrameworkCore | `Microsoft.EntityFrameworkCore` |
| Entity Framework Core Migrations| `Microsoft.EntityFrameworkCore.Migrations` |
- **Schema:** `irby`
- **Table:** `PayrollMatch`
---
## 3. Key Functional Areas β
### 3.1 Up Method
```csharp
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "EmployeeNumber",
schema: "irby",
table: "PayrollMatch",
type: "nvarchar(max)",
nullable: true);
}
- Purpose:
- Add a nullable
nvarchar(max)column namedEmployeeNumbertoirby.PayrollMatch.
- Add a nullable
- Why Nullable?
- Maintains backward compatibility with existing records.
3.2 Down Method
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "EmployeeNumber",
schema: "irby",
table: "PayrollMatch");
}
- Purpose:
- Remove the
EmployeeNumbercolumn, effectively rolling back the migration.
- Remove the
4. Code Structure & UML Diagrams π
The migration class inherits from Migration and overrides two methods:
classDiagram
class Migration {
<<abstract>>
+Up(MigrationBuilder)
+Down(MigrationBuilder)
}
class AddColumnToPayrollMatchTable {
+Up(MigrationBuilder)
+Down(MigrationBuilder)
}
Migration <|-- AddColumnToPayrollMatchTable
File Structure
IrbyImaging.DbContext/
βββ Migrations/
βββ 20250211130437_AddColumnToPayrollMatchTable.cs
5. View Models & Data Models π¦
This migration does not directly define view models or data modelsβit only alters the database schema.
After applying, update your EF CoreDbContextentityPayrollMatch:
public class PayrollMatch
{
public int Id { get; set; }
// ... existing properties ...
// New property
public string? EmployeeNumber { get; set; }
}
6. Helper Methods & Utilities π οΈ
- MigrationBuilder:
AddColumn<T>DropColumn
No additional utilities or extension methods are used here.
7. Error Handling & Rollbacks π
- Automatic Rollback: EF Core tracks applied migrations.
- Manual Rollback:
dotnet ef database update LastGoodMigrationName - Exceptions:
- Column already exists β Migration fails at startup.
- Schema/table not found β Migration fails.
8. API Endpoints Overview π§
Not Applicable
This is a database migration file and does not expose any HTTP/API endpoints.
9. Third-party Integrations π
- Entity Framework Core β official ORM for .NET.
- SQL Server (or any RDBMS) β supports
nvarchar(max).
10. Sample Code Blocks & CLI Usage π»
Creating a Migration
# At solution root
dotnet ef migrations add AddColumnToPayrollMatchTable \
--context IrbyImagingDbContext \
--schema irby \
--output-dir Migrations
Applying Migrations
dotnet ef database update --context IrbyImagingDbContext
11. Usage Examples π
-
Developer Flow
- Pull latest code
dotnet ef migrations add AddColumnToPayrollMatchTabledotnet ef database update- Verify
EmployeeNumberinPayrollMatchtable
-
Rollback Scenario
dotnet ef database update 20250211130000_PreviousMigration
12. Conclusion π―
-
What We Achieved:
- Seamlessly added a new column to support employee tracking in payroll reconciliation.
- Provided a reversible migration to maintain schema version control.
-
Next Steps:
- Update your C# entity (
PayrollMatch) to includeEmployeeNumber. - Adapt any service, repository, or UI layer that consumes this entity.
- Add tests to cover scenarios when
EmployeeNumberis present or null.
- Update your C# entity (
Happy Coding! β¨