π Migration Documentation
File: IrbyImaging.DbContext/Migrations/20240724175502_AddRejectionCommentMechanicTimesheet.cs
π Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Key Functional Areas
- Migration Class Structure
- 4.1 Up() Method
- 4.2 Down() Method
- Database Impact
- Entity Model Changes
- Usage Examples
- EF Core CLI Commands
- UML Diagram
- Conclusion
π 1. Overview & Purpose
This Entity Framework Core Migration adds a new column, RejectionComment, to the MechanicTimesheet table under the irby schema.
- Why? βοΈ When a timesheet entry is rejected by a supervisor or system check, we need a place to store the reason for rejection.
- Result: A nullable
nvarchar(max)column that holds freeβform text comments.
π 2. Dependencies & Technologies
| Technology / Package | Version (Approx.) | Purpose |
|---|---|---|
| .NET SDK | 6.0+ | Core runtime |
| Microsoft.EntityFrameworkCore | 6.0+ | ORM framework |
| Microsoft.EntityFrameworkCore.Design | 6.0+ | Migrations & design-time services |
| SQL Server (or compatible) | 2012+ | Relational database |
βοΈ 3. Key Functional Areas
- Schema Modification β introduces a new column.
- Migration Reversible β defines both
Up()andDown()to apply or rollback changes. - Database Safety β marking the column as nullable to avoid breaking existing data.
π 4. Migration Class Structure
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace IrbyImaging.DbContext.Migrations
{
/// <inheritdoc />
public partial class AddRejectionCommentMechanicTimesheet : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "RejectionComment",
schema: "irby",
table: "MechanicTimesheet",
type: "nvarchar(max)",
nullable: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "RejectionComment",
schema: "irby",
table: "MechanicTimesheet");
}
}
}
4.1 Up() Method
- Purpose: Applies the schema change.
- Action:
migrationBuilder.AddColumn<string>( name: "RejectionComment", schema: "irby", table: "MechanicTimesheet", type: "nvarchar(max)", nullable: true); - Result:
- New column RejectionComment of type
string(nvarchar(max)) - Permits NULL values to avoid disrupting existing records.
- New column RejectionComment of type
4.2 Down() Method
- Purpose: Rolls back the
Up()changes. - Action:
migrationBuilder.DropColumn( name: "RejectionComment", schema: "irby", table: "MechanicTimesheet"); - Result: Restores the
MechanicTimesheettable to its prior state.
π 5. Database Impact
| Table | Column | Type | Nullable | Description |
|---|---|---|---|---|
| irby.MechanicTimesheet | RejectionComment | nvarchar(max) | Yes | Reason provided when timesheet is rejected |
Note: Existing rows will simply have
NULLin this column until explicitly set.
π§© 6. Entity Model Changes
When you scaffold or manually update your C# Entity, add a matching property:
namespace IrbyImaging.Domain.Entities
{
public class MechanicTimesheet
{
public int Id { get; set; }
// ... other properties ...
/// <summary>
/// Comment explaining why this timesheet was rejected.
/// </summary>
public string? RejectionComment { get; set; }
}
}
Donβt forget to update your DbContext if you use Fluent API configuration:
modelBuilder.Entity<MechanicTimesheet>(entity =>
{
entity.ToTable("MechanicTimesheet", "irby");
entity.Property(e => e.RejectionComment)
.HasColumnType("nvarchar(max)")
.IsRequired(false);
});
πββοΈ 7. Usage Examples
7.1 Adding a Rejection Comment
using (var context = new IrbyImagingDbContext())
{
var timesheet = context.MechanicTimesheets.Find(123);
if (timesheet != null)
{
timesheet.RejectionComment = "Missing supervisor signature.";
context.SaveChanges();
}
}
7.2 Querying Rejected Timesheets
var rejectedList = context.MechanicTimesheets
.Where(ts => ts.RejectionComment != null)
.ToList();
π» 8. EF Core CLI Commands
- Add Migration
dotnet ef migrations add AddRejectionCommentMechanicTimesheet \ --context IrbyImagingDbContext - Apply to Database
dotnet ef database update - Rollback (to previous migration)
dotnet ef database update LastGoodMigrationName
π 9. UML Diagram
classDiagram
class MechanicTimesheet {
+int Id
+DateTime Date
+decimal HoursWorked
+string? RejectionComment
}
Legend:
+means public.?indicates a nullable property.
π 10. Conclusion
This migration is a safe, incremental change that enhances the MechanicTimesheet domain by capturing rejection reasons. By following the Up/Down pattern, it ensures smooth rollouts and easy rollbacks.
Next Steps
- Update any UI or API layers to display/edit this new field.
- Write validation rules if needed (e.g., min/max length).
- Add unit/integration tests around rejection workflows.
Happy coding! β¨π