πŸ“š Migration Documentation

File: IrbyImaging.DbContext/Migrations/20240724175502_AddRejectionCommentMechanicTimesheet.cs


πŸ“‘ Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Key Functional Areas
  4. Migration Class Structure
  5. Database Impact
  6. Entity Model Changes
  7. Usage Examples
  8. EF Core CLI Commands
  9. UML Diagram
  10. 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() and Down() 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.

4.2 Down() Method

  • Purpose: Rolls back the Up() changes.
  • Action:
    migrationBuilder.DropColumn(
        name: "RejectionComment",
        schema: "irby",
        table: "MechanicTimesheet");
    
  • Result: Restores the MechanicTimesheet table 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 NULL in 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

  1. Add Migration
    dotnet ef migrations add AddRejectionCommentMechanicTimesheet \
        --context IrbyImagingDbContext
    
  2. Apply to Database
    dotnet ef database update
    
  3. 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! βœ¨πŸš€