π Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Migration File Structure
- Key Functional Areas
- 4.1 Up Method
- 4.2 Down Method
- Data Model Impact
- Usage Examples
- UML Class Diagram
- Conclusion
π 1. Overview & Purpose
The 20240201184940_UpdateEmployeeTableWithNewColumns3 migration is part of the Entity Framework Core code-first approach. Its primary goal is to add a new FieldOfficeId column (of type GUID) to the JobAndFieldOfficeMapping table, enabling stronger referential integrity by linking jobs to a specific field office.
Why this Migration?
- Ensure each job-office mapping includes a
FieldOfficeId. - Support new business logic around multi-office assignments.
- Prepare the schema for future foreignβkey relationships or lookups.
π οΈ 2. Dependencies & Technologies
| Technology | Version / Details |
|---|---|
| .NET SDK | 6.x, 7.x (or higher) |
| Entity Framework Core | Microsoft.EntityFrameworkCore (EF Core) |
| C# | 10+ |
Note: Migrations are generated via the
dotnet efCLI or Package Manager Console (PMC).
π 3. Migration File Structure
IrbyImaging.DbContext
ββ Migrations
ββ 20240201184940_UpdateEmployeeTableWithNewColumns3.cs
Namespace:
namespace IrbyImaging.DbContext.Migrations
Class Signature:
public partial class UpdateEmployeeTableWithNewColumns3 : Migration
This class inherits from Microsoft.EntityFrameworkCore.Migrations.Migration.
π 4. Key Functional Areas
4.1 Up Method
- Purpose: Define the changes to apply when the migration runs (
Update-Databaseordotnet ef database update). - Action: Adds a non-nullable
GUIDcolumn namedFieldOfficeIdto theJobAndFieldOfficeMappingtable, with a default value of00000000-0000-0000-0000-000000000000.
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<Guid>(
name: "FieldOfficeId",
table: "JobAndFieldOfficeMapping",
type: "uniqueidentifier",
nullable: false,
defaultValue: new Guid("00000000-0000-0000-0000-000000000000"));
}
| Parameter | Description |
|---|---|
| name | "FieldOfficeId" β Column name |
| table | "JobAndFieldOfficeMapping" β Target table |
| type | "uniqueidentifier" β SQL Server GUID type |
| nullable | false β Column cannot be null |
| defaultValue | Guid.Empty β Prevents issues on existing rows |
4.2 Down Method
- Purpose: Revert the schema changes when rolling back the migration (
dotnet ef database update <PreviousMigration>). - Action: Drops the
FieldOfficeIdcolumn fromJobAndFieldOfficeMapping.
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "FieldOfficeId",
table: "JobAndFieldOfficeMapping");
}
ποΈ 5. Data Model Impact
While this file itself does not define the entity class, it affects the underlying model:
public class JobAndFieldOfficeMapping
{
public Guid Id { get; set; }
public Guid JobId { get; set; }
public Guid FieldOfficeId { get; set; } // β New property to map to DB column
// ... other properties ...
}
After applying the migration, ensure your DbContext and entity classes include the new
FieldOfficeIdproperty.
βοΈ 6. Usage Examples
6.1 Generating the Migration
dotnet ef migrations add UpdateEmployeeTableWithNewColumns3 \
--project IrbyImaging.DbContext \
--startup-project IrbyImaging.Api
6.2 Applying & Rolling Back
- Apply migration:
dotnet ef database update - Rollback to previous migration:
dotnet ef database update 20240115094722_PreviousMigrationName
π 7. UML Class Diagram
@startuml
class Migration {
+Up(migrationBuilder)
+Down(migrationBuilder)
}
class UpdateEmployeeTableWithNewColumns3 {
+Up(migrationBuilder)
+Down(migrationBuilder)
}
Migration <|-- UpdateEmployeeTableWithNewColumns3
@enduml
Above: A simple class diagram showing inheritance.
β 8. Conclusion
- What we did: Added a non-nullable
FieldOfficeIdGUID column to theJobAndFieldOfficeMappingtable. - Why it matters: Strengthens the data model, enabling explicit linkages between jobs and field offices.
- Next steps:
- Update your Entity Classes and DbContext to include
FieldOfficeId. - Recompile and run integration tests.
- Publish the updated database schema in staging/production.
- Update your Entity Classes and DbContext to include
π Now your database is ready to handle fieldβoffice assignments more robustly!