π AddTypeCodeToWorkOrder Migration Documentation
File:
IrbyImaging.DbContext/Migrations/20240805140229_AddTypeCodeToWorkOrder.cs
Namespace:IrbyImaging.DbContext.Migrations
Migration:AddTypeCodeToWorkOrder
π Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Key Functional Areas
- Data Model Changes
- Migration Implementation Details
- 5.1 Up Method
- 5.2 Down Method
- UML Diagram
- Sample Commands & Usage Examples
- Conclusion
1. Overview & Purpose
This Entity Framework Core migration adds a new column named WorkOrderTypeCode to the existing WorkOrder table in the irby schema.
-
π§ Why?
- To store a code representing the type of each work order (e.g., Repair, Inspection, Installation).
- Enables filtering, reporting, and business logic based on work order types.
-
π― When to Use?
- After generating the migration, run
dotnet ef database update(or Package Manager Console equivalent) to apply the schema change. - Update your C# domain model and any data access code to read/write this new column.
- After generating the migration, run
2. Dependencies & Technologies
| Technology | Version / Notes |
|---|---|
| .NET SDK | β₯ 6.0 (or your projectβs target framework) |
| Entity Framework Core | β₯ 7.0 (for MigrationBuilder APIs) |
| C# | 10.0+ |
| Database | SQL Server (nvarchar(max)) |
Tip: Ensure your project has a reference to
Microsoft.EntityFrameworkCore.Designfor migrations to scaffold correctly.
3. Key Functional Areas
- Schema Evolution
- Introduces one new nullable column β
WorkOrderTypeCode.
- Introduces one new nullable column β
- Reversibility
- Implements
Down(...)method to roll back the change (drop the column).
- Implements
4. Data Model Changes
After applying this migration, your WorkOrder entity (in your DbContext) should include:
public class WorkOrder
{
public int Id { get; set; }
// ... existing properties ...
/// <summary>
/// Code representing the Work Order type (e.g., "INSPECT", "REPAIR").
/// </summary>
public string? WorkOrderTypeCode { get; set; }
}
| Column Name | Type | Nullable | Default | Description |
|---|---|---|---|---|
| WorkOrderTypeCode | nvarchar(max) |
β Yes | NULL |
Stores the type code for a work order. |
5. Migration Implementation Details
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace IrbyImaging.DbContext.Migrations
{
/// <inheritdoc />
public partial class AddTypeCodeToWorkOrder : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "WorkOrderTypeCode",
schema: "irby",
table: "WorkOrder",
type: "nvarchar(max)",
nullable: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "WorkOrderTypeCode",
schema: "irby",
table: "WorkOrder");
}
}
}
5.1 Up Method
- Purpose: Adds the
WorkOrderTypeCodecolumn. - Key API Call:
migrationBuilder.AddColumn<string>( name: "WorkOrderTypeCode", schema: "irby", table: "WorkOrder", type: "nvarchar(max)", nullable: true);
5.2 Down Method
- Purpose: Reverts the schema to its previous state by dropping the column.
- Key API Call:
migrationBuilder.DropColumn( name: "WorkOrderTypeCode", schema: "irby", table: "WorkOrder");
6. UML Diagram
Below is a Mermaid class diagram showing the WorkOrder entity before and after the migration:
classDiagram
class WorkOrder {
+int Id
+string? ExistingProperty1
+DateTime CreatedDate
+string? WorkOrderTypeCode <<added>>
}
%% Schema: irby.WorkOrder
7. Sample Commands & Usage Examples
7.1 Applying the Migration
-
Via .NET CLI
dotnet ef migrations add AddTypeCodeToWorkOrder dotnet ef database update -
Via Package Manager Console (Visual Studio)
Add-Migration AddTypeCodeToWorkOrder Update-Database
7.2 Reading / Writing the New Column
using var context = new IrbyDbContext();
// Create a new work order
var wo = new WorkOrder {
/* other properties */,
WorkOrderTypeCode = "REPAIR"
};
context.WorkOrders.Add(wo);
context.SaveChanges();
// Query by type code
var repairs = context.WorkOrders
.Where(w => w.WorkOrderTypeCode == "REPAIR")
.ToList();
8. Conclusion
β¨ AddTypeCodeToWorkOrder is a straightforward, reversible EF Core migration that enhances the WorkOrder table with a new WorkOrderTypeCode column.
- Ensures backwards compatibility via the
Downmethod. - Provides flexibility for future business logic around work order types.
Next Steps:
- Update your domain models and DTOs/ViewModels.
- Adjust repository or service layer methods if filtering by work order type.
- Deploy migration to staging/production to begin capturing type codes in new records.
Happy coding! π