📋 Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Key Functional Areas
- Data Model Impact
- How It Works
- Usage & Examples
- UML Diagrams
- Error Handling & Migration Rollback
- Conclusion 🎉
Overview & Purpose
File
IrbyImaging.DbContext/Migrations/20230718150337_AddUARApproverToEmployeeTable.cs
This Entity Framework Core migration adds a new boolean flag, UARApprover, to the Employee table in the irby schema.
🔑 Why?
In the Irby Imaging domain, certain employees are designated as UAR (User Access Request) approvers. This flag enables the application to programmatically determine who has the authority to approve user access requests.
Dependencies & Technologies
| Technology / Package | Version / Note |
|---|---|
| .NET Core / .NET 6+ | C# language, project target framework |
| Microsoft.EntityFrameworkCore | For migrations, DbContext, MigrationBuilder |
irby DB schema |
Custom schema within SQL Server / PostgreSQL |
⚙️ Key Namespace
using Microsoft.EntityFrameworkCore.Migrations;
Key Functional Areas
- Migration Class Declaration
public partial class AddUARApproverToEmployeeTable : Migration
- Up Method
- Defines how the database schema changes when the migration is applied.
- Adds a new column
UARApprover(boolean) to theEmployeetable.
- Down Method
- Defines how to revert the changes if the migration is rolled back.
- Drops the
UARApprovercolumn from theEmployeetable.
Data Model Impact
| Table | New Column | Type | Nullable | Default Value | Description |
|---|---|---|---|---|---|
| Employee | UARApprover | bit | No | false |
Indicates if employee can approve UARs. |
💡 Note:
After applying this migration, every existing record inEmployeewill haveUARApprover = falseby default.
How It Works
public partial class AddUARApproverToEmployeeTable : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
// 1️⃣ Add the UARApprover column (bool) to Employee in irby schema
migrationBuilder.AddColumn<bool>(
name: "UARApprover",
schema: "irby",
table: "Employee",
type: "bit",
nullable: false,
defaultValue: false);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
// 2️⃣ Remove the UARApprover column if rolling back
migrationBuilder.DropColumn(
name: "UARApprover",
schema: "irby",
table: "Employee");
}
}
Up- Uses
migrationBuilder.AddColumn<T>()to extend the schema.
- Uses
Down- Uses
migrationBuilder.DropColumn()to undo the change.
- Uses
These methods form the forward and backward steps of your version-controlled database schema.
Usage & Examples
1. Generating the Migration
dotnet ef migrations add AddUARApproverToEmployeeTable \
--project IrbyImaging.DbContext \
--output-dir Migrations
2. Applying to Database
dotnet ef database update --project IrbyImaging.DbContext
3. Accessing the New Property in Code
public class EmployeeService
{
private readonly IrbyDbContext _context;
public EmployeeService(IrbyDbContext context)
{
_context = context;
}
public async Task<bool> CanApproveUAR(int employeeId)
{
var employee = await _context.Employees.FindAsync(employeeId);
return employee?.UARApprover ?? false;
}
}
UML Diagrams
Class Diagram (Mermaid)
classDiagram
class Employee {
+int EmployeeId
+string FirstName
+string LastName
+bool UARApprover <<new>>
}
Migration Flow
flowchart LR
A[(Start)] --> B[Apply Migrations]
B --> C[AddColumn UARApprover]
C --> D[(Database Updated)]
D --> E{Rollback?}
E -- Yes --> F[DropColumn UARApprover]
E -- No --> G[(End)]
Error Handling & Migration Rollback
-
Migration Conflicts
- If another migration has modified the
Employeetable concurrently, you may encounter conflicts. - Resolution: Re-scaffold the conflicting migration or manually adjust
Up/Downscripts.
- If another migration has modified the
-
Rollback
- To rollback one step:
dotnet ef database update <PreviousMigrationName> - The
Downmethod will removeUARApprover.
- To rollback one step:
Conclusion 🎉
This migration is a lightweight, zero-downtime update to your Employee data model—adding the UARApprover flag to empower new business logic around user access request approvals.
- Seamlessly applies via EF Core CLI
- Safe rollback with the
Downmethod - Enables clean, type-safe access in your C# services
By following the Usage & Examples section, you can integrate this schema change into your continuous delivery pipeline and update your application logic to leverage the new flag.
Enjoy the new capability and happy coding! 🚀