π AddWorkOrderType Migration Documentation
π Table of Contents
- π― Overview & Purpose
- π οΈ Dependencies & Technologies
- π Entity Framework Core Migrations Primer
- π§ Migration File: AddWorkOrderType
- 4.1. π UML Class Diagram
- 4.2. π Up Method
- 4.3. β¬οΈ Down Method
- π§© Data Model Changes
- π Applying the Migration
- π Rolling Back the Migration
- π Integration with DbContext
- β οΈ Error Handling & Considerations
- π Conclusion
π― Overview & Purpose
The AddWorkOrderType migration introduces a new column, WorkOrderType, to the MechanicTimesheet table in the irby schema.
This enhancement allows tracking of the type/category of work order each mechanic records, improving reporting and filtering capabilities.
π οΈ Dependencies & Technologies
| Technology | Version | Purpose |
|---|---|---|
| .NET SDK | 6.0 / 7.0+ | Application platform |
| Microsoft.EntityFrameworkCore | 6.0 / 7.0+ | ORM & migrations |
| Microsoft.EntityFrameworkCore.SqlServer | 6.0 / 7.0+ | SQL Server provider |
| Microsoft.EntityFrameworkCore.Tools | 6.0 / 7.0+ | CLI tooling for EF Core |
π Entity Framework Core Migrations Primer
Entity Framework Core Migrations let you incrementally update your database schema to keep it in sync with your data model. Each migration class:
- Inherits from
Migration. - Implements an
Upmethod to apply schema changes. - Implements a
Downmethod to revert them.
π§ Migration File: AddWorkOrderType
Location:
IrbyImaging.DbContext/Migrations/20240801143541_AddWorkOrderType.cs
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace IrbyImaging.DbContext.Migrations
{
/// <inheritdoc />
public partial class AddWorkOrderType : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "WorkOrderType",
schema: "irby",
table: "MechanicTimesheet",
type: "nvarchar(max)",
nullable: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "WorkOrderType",
schema: "irby",
table: "MechanicTimesheet");
}
}
}
π UML Class Diagram
classDiagram
Migration <|-- AddWorkOrderType
class Migration {
<<abstract>>
+Up(migrationBuilder: MigrationBuilder)
+Down(migrationBuilder: MigrationBuilder)
}
class AddWorkOrderType {
+Up(migrationBuilder: MigrationBuilder)
+Down(migrationBuilder: MigrationBuilder)
}
π Up Method
- Purpose: Adds the new column to the table.
- Key Call:
migrationBuilder.AddColumn<string>( name: "WorkOrderType", schema: "irby", table: "MechanicTimesheet", type: "nvarchar(max)", nullable: true); - Effects:
- Column Name:
WorkOrderType - Data Type:
nvarchar(max) - Nullable:
true
- Column Name:
β¬οΈ Down Method
- Purpose: Safely rolls back the schema change by dropping the column.
- Key Call:
migrationBuilder.DropColumn( name: "WorkOrderType", schema: "irby", table: "MechanicTimesheet");
π§© Data Model Changes
To fully leverage the new column, update your MechanicTimesheet entity:
namespace IrbyImaging.DbContext.Models
{
public class MechanicTimesheet
{
public int Id { get; set; }
public int MechanicId { get; set; }
public DateTime Date { get; set; }
public decimal HoursWorked { get; set; }
// π New property
public string? WorkOrderType { get; set; }
}
}
Updated Table Schema (irby.MechanicTimesheet):
| Column Name | Data Type | Nullable | Description |
|---|---|---|---|
| Id | int | No | Primary key |
| MechanicId | int | No | Foreign key to Mechanic |
| Date | datetime2 | No | Date of entry |
| HoursWorked | decimal(5,2) | No | Hours logged |
| WorkOrderType | nvarchar(max) | Yes | Category of work order |
π Applying the Migration
- Add & Build
dotnet build - Update Database
dotnet ef database update - Verify
- Inspect the
MechanicTimesheettable in SQL Server (e.g., via SSMS) - Confirm presence of the
WorkOrderTypecolumn
- Inspect the
π Rolling Back the Migration
To revert:
dotnet ef database update 20240601120000_PreviousMigrationName
Replace 20240601120000_PreviousMigrationName with the target earlier migration. This executes the Down method.
π Integration with DbContext
Your DbContext remains unchanged, but ensure your DbSet<MechanicTimesheet> is present:
public class IrbyDbContext : DbContext
{
public DbSet<MechanicTimesheet> MechanicTimesheet { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("irby");
// Other model configurations...
}
}
β οΈ Error Handling & Considerations
- Nullability: Because
WorkOrderTypeis nullable, existing rows wonβt break. - Large Text:
nvarchar(max)can store very long stringsβensure UI/validation handles it. - Schema Sync: Always check in your migration file to source control to prevent drift in collaborative teams.
π Conclusion
The AddWorkOrderType migration is a simple yet impactful update that enables richer data capture on mechanic timesheets by categorizing work orders. By following the steps above, you can seamlessly apply, use, and (if necessary) roll back this change in your database environment.
Happy coding! π