๐๏ธ Documentation: 20250303183821_UpdateColumnTypeOnPayrollFringe2.cs
Path:
IrbyImaging.DbContext/Migrations/20250303183821_UpdateColumnTypeOnPayrollFringe2.cs
๐ Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Migration Class Structure
- Up Method โ Applying the Migration
- Down Method โ Reverting the Migration
- Usage Example
- UML Class Diagram
- Best Practices & Considerations
- Conclusion
1. Overview & Purpose
This migration class is part of the Entity Framework Core code-first approach. It modifies the database schema for the PayrollFringe table by introducing a new non-nullable JobId column of type uniqueidentifier (GUID).
**Key Goals:
**
- ๐ Add a
JobIdcolumn toPayrollFringe. - ๐งน Provide a clean Up script and corresponding Down script.
- ๐ ๏ธ Ensure the database can be rolled forward or backward seamlessly.
2. Dependencies & Technologies
| Technology/Package | Version / Use |
|---|---|
| .NET 6+ (or compatible) | Entity Framework Core migrations mechanism |
| Microsoft.EntityFrameworkCore.Migrations | Provides MigrationBuilder, Migration base class |
SQL Server (Schema: irby) |
Underlying database engine (type uniqueidentifier) |
3. Migration Class Structure
namespace IrbyImaging.DbContext.Migrations
{
public partial class UpdateColumnTypeOnPayrollFringe2 : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
// -- Add new column logic
}
protected override void Down(MigrationBuilder migrationBuilder)
{
// -- Revert column logic
}
}
}
- Namespace:
IrbyImaging.DbContext.Migrations - Class:
UpdateColumnTypeOnPayrollFringe2inherits fromMigration - Partial Class: Typically generated by EF Core tooling; may be extended if needed.
4. Up Method โ Applying the Migration
The Up method defines the changes to apply when running:
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<Guid>(
name: "JobId",
schema: "irby",
table: "PayrollFringe",
type: "uniqueidentifier",
nullable: false,
defaultValue: new Guid("00000000-0000-0000-0000-000000000000"));
}
What happens?
- ๐ Adds a column named
JobId - ๐ Data type:
uniqueidentifier(GUID) - ๐ซ Non-nullable, with a default value of
00000000-0000-0000-0000-000000000000 - ๐ท๏ธ Table:
irby.PayrollFringe
Why a default GUID?
- Ensures existing rows receive a valid (albeit zeroed) GUID.
- Prevents migration failures due to non-null constraints.
5. Down Method โ Reverting the Migration
To reverse the above change, the Down method executes:
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "JobId",
schema: "irby",
table: "PayrollFringe");
}
- ๐๏ธ Drops the
JobIdcolumn - Safely returns the schema to its previous state
6. Usage Example
- Add Migration
dotnet ef migrations add UpdateColumnTypeOnPayrollFringe2 - Apply Migration
dotnet ef database update - Verify
You should seeSELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'irby' AND TABLE_NAME = 'PayrollFringe';JobIdlisted as auniqueidentifier, NO nulls allowed.
7. UML Class Diagram
+------------------------------------+
| <<Migration>> |
| Migration |
+------------------------------------+
โฒ
|
+------------------------------------+
| UpdateColumnTypeOnPayrollFringe2 |
+------------------------------------+
| + Up(migrationBuilder) |
| + Down(migrationBuilder) |
+------------------------------------+
- Inheritance:
UpdateColumnTypeOnPayrollFringe2โMigration - Methods:
Up/Down
8. Best Practices & Considerations
- Backup First ๐
Always backup your database before applying migrations in production. - Test Locally โ
Rundotnet ef database updateagainst a test database. - Default Values โ๏ธ
Choose sensible defaults to avoid introducing invalid data. - Naming Conventions ๐ท๏ธ
Keep migration names descriptive & timestamped. - Idempotency ๐
EnsureDownmethod cleanly reverts changes. - Review Generated SQL ๐
Use--verboseflag or inspect EFโs SQL to confirm operations.
9. Conclusion
This migration script is a small but critical part of your code-first database evolution. It demonstrates:
- How to add a new non-nullable column via EF Core
- How to gracefully rollback changes
- The importance of default values for existing rows
By following the patterns shown here, you ensure your database schema stays in sync with your domain models, enabling safe and repeatable deployments. ๐
End of Documentation ๐