πŸ“ Documentation: AddReferenceNumberToTimesheet Migration


File Path:

IrbyImaging.DbContext/Migrations/20240802133735_AddReferenceNumberToTimesheet.cs

Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Key Functional Areas
  4. Code Walkthrough
    4.1. The Up Method
    4.2. The Down Method
  5. Database Schema Impact
  6. Usage & Sample Commands
  7. Best Practices & Error Handling
  8. UML Diagram
  9. Conclusion

Overview & Purpose

This Entity Framework Core migration AddReferenceNumberToTimesheet automates the addition of a new, non-nullable integer column named ReferenceNumber to the MechanicTimesheet table within the "irby" schema.

πŸš€ Primary goals:

  • Extend the data model to store a reference number for each timesheet.
  • Ensure all existing records get a default value (0) to maintain data integrity.

Dependencies & Technologies

Technology / Package Version Purpose
.NET / C# β‰₯ .NET 6.0 Base platform
Microsoft.EntityFrameworkCore β‰₯ 6.0.0 ORM & Migration support
SQL Server (or compatible) N/A Target database

Note: This migration was scaffolded by EF Core’s dotnet ef migrations add command.


Key Functional Areas

  • Migration Class Definition
    Declares a partial class inheriting from Migration.
  • Up(MigrationBuilder)
    Defines schema changes to apply (adding the new column).
  • Down(MigrationBuilder)
    Provides a rollback path (dropping the column).

Code Walkthrough

using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace IrbyImaging.DbContext.Migrations
{
    /// <inheritdoc />
    public partial class AddReferenceNumberToTimesheet : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<int>(
                name: "ReferenceNumber",
                schema: "irby",
                table: "MechanicTimesheet",
                type: "int",
                nullable: false,
                defaultValue: 0);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "ReferenceNumber",
                schema: "irby",
                table: "MechanicTimesheet");
        }
    }
}

The Up Method

  • Signature
    protected override void Up(MigrationBuilder migrationBuilder)
    
  • Behavior
    1. Calls migrationBuilder.AddColumn<int>(…).
    2. Specifies:
      • name: "ReferenceNumber"
      • schema: "irby"
      • table: "MechanicTimesheet"
      • type: "int"
      • nullable: false
      • defaultValue: 0
  • Result
    Adds a non-nullable integer column. All existing rows receive 0 by default.

The Down Method

  • Signature
    protected override void Down(MigrationBuilder migrationBuilder)
    
  • Behavior
    1. Calls migrationBuilder.DropColumn(…).
    2. Drops ReferenceNumber from the specified schema/table.
  • Result
    Reverts the schema to its previous state by removing the column.

Database Schema Impact

Before Migration

irby.MechanicTimesheet
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ TimesheetId β”‚ MechanicId β”‚ Date   β”‚ …
β””β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

After Migration

irby.MechanicTimesheet
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ TimesheetId   β”‚ MechanicId   β”‚ Date    β”‚ ReferenceNumber β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 1             β”‚ 42           β”‚ …       β”‚ 0              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Column Type Nullable Default
ReferenceNumber int No 0

Usage & Sample Commands

Adding & Applying the Migration

  1. Scaffold (already done)

    dotnet ef migrations add AddReferenceNumberToTimesheet \
      --namespace IrbyImaging.DbContext.Migrations
    
  2. Update Database

    dotnet ef database update
    

Entity Model Adjustment

After the migration, update your entity class:

namespace IrbyImaging.Models
{
    public class MechanicTimesheet
    {
        public int TimesheetId { get; set; }
        public int MechanicId { get; set; }
        public DateTime Date { get; set; }

        // πŸ†• New property
        public int ReferenceNumber { get; set; }
    }
}

Sample Query

var sheet = await context.MechanicTimesheets
    .Where(t => t.ReferenceNumber > 1000)
    .ToListAsync();

Best Practices & Error Handling

  • βœ… Always test migrations in a staging environment before production.
  • βœ… Backup your database or use source-controlled migrations.
  • ⚠️ Dropping columns (in Down) may cause data loss. Use with caution.

UML Diagram

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚       Migration (EF Core)       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
          β–²
          β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ AddReferenceNumberToTimesheet   β”‚
β”‚  - inherits Migration           β”‚
β”‚  + Up(MigrationBuilder)         β”‚
β”‚  + Down(MigrationBuilder)       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Conclusion

This migration adds robust tracking for each mechanic’s timesheet via a new ReferenceNumber column. By following EF Core best practicesβ€”scaffolding, testing, and updating the data modelβ€”you maintain a consistent schema evolution workflow. πŸŽ‰


Next Steps:

  1. Merge the migration into your main branch.
  2. Deploy to staging/production.
  3. Update any relevant business logic to leverage the new ReferenceNumber.

Thank you for using IrbyImaging’s data-layer standards! πŸ˜ƒ