πŸ“š IrbyImaging.DbContext/Migrations/20240701202611_UpdateMechanicTimesheet.cs Documentation


πŸ“‘ Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Migration File Structure
  4. Key Functional Areas
  5. UML Class Diagram
  6. Applying & Rolling Back Migrations
  7. Sample Code Blocks
  8. Conclusion

1. Overview & Purpose

Filename:
20240701202611_UpdateMechanicTimesheet.cs

Namespace:
IrbyImaging.DbContext.Migrations

This Entity Framework Core migration adds a new column, EmployeeName, to the MechanicTimesheet table in the irby schema, and provides a way to remove it if the migration is rolled back.

Aspect Details
Migration Name UpdateMechanicTimesheet
Target Table irby.MechanicTimesheet
Column Added EmployeeName (nvarchar(max), nullable)
Migrations Used AddColumn, DropColumn
Created On 2024-07-01 20:26:11 UTC

2. Dependencies & Technologies

Dependency/Tech Purpose
Microsoft.EntityFrameworkCore.Migrations Core migrations API for code-first database changes
C# (.NET 6+/7+) Language & runtime for executing migrations

πŸš€ Note: Ensure your project references Microsoft.EntityFrameworkCore and has the proper DbContext configured.


3. Migration File Structure

using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace IrbyImaging.DbContext.Migrations
{
    /// <inheritdoc />
    public partial class UpdateMechanicTimesheet : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            // Adds EmployeeName column
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            // Drops EmployeeName column
        }
    }
}

Key Points:

  • The class inherits from Migration.
  • Two methods define the schema change: Up (apply) and Down (revert).
  • #nullable disable ensures compatibility regardless of nullable reference settings.

4. Key Functional Areas

Up() Method – Applying the Change 🚧

Adds a new string column EmployeeName to the MechanicTimesheet table:

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.AddColumn<string>(
        name: "EmployeeName",
        schema: "irby",
        table: "MechanicTimesheet",
        type: "nvarchar(max)",
        nullable: true);
}
  • AddColumn<string>: Specifies a string (nvarchar(max)) column.
  • nullable: true: The column allows NULL values for backward compatibility.

What It Does

  1. Alters the database schema.
  2. Injects the new column into all existing and future MechanicTimesheet records.
  3. Does not enforce a default valueβ€”existing rows will have EmployeeName = NULL.

Down() Method – Rolling Back πŸ”„

Removes the previously added EmployeeName column:

protected override void Down(MigrationBuilder migrationBuilder)
{
    migrationBuilder.DropColumn(
        name: "EmployeeName",
        schema: "irby",
        table: "MechanicTimesheet");
}
  • DropColumn: Safely deletes the column if the migration is reversed.
  • Ensures the database can revert to its prior state.

5. UML Class Diagram

classDiagram
    Migration <|-- UpdateMechanicTimesheet
    class Migration {
        <<abstract>>
        +Up(MigrationBuilder)
        +Down(MigrationBuilder)
    }
    class UpdateMechanicTimesheet {
        +Up(MigrationBuilder)
        +Down(MigrationBuilder)
    }
  • Migration is the base class provided by EF Core.
  • UpdateMechanicTimesheet implements the Up and Down overrides.

6. Applying & Rolling Back Migrations

Applying the Migration

# From your project directory:
dotnet ef migrations add UpdateMechanicTimesheet
dotnet ef database update
  • ef migrations add: Generates a new migration file (timestamp + name).
  • ef database update: Applies all pending migrations, including UpdateMechanicTimesheet.

Rolling Back a Migration

# Target the previous migration (for example, β€œ20240630_PreviousMigration”):
dotnet ef database update 20240630_PreviousMigration
  • This command will invoke the Down method of UpdateMechanicTimesheet to remove EmployeeName.

7. Sample Code Blocks

Before Migration: MechanicTimesheet Model

public class MechanicTimesheet
{
    public int Id { get; set; }
    public DateTime Date { get; set; }
    // No EmployeeName yet
    public int HoursWorked { get; set; }
}

After Migration: MechanicTimesheet Model

public class MechanicTimesheet
{
    public int Id { get; set; }
    public DateTime Date { get; set; }
    public string? EmployeeName { get; set; }  // New nullable property
    public int HoursWorked { get; set; }
}

8. Conclusion 🎯

  • Purpose Achieved: Safely extends the MechanicTimesheet table with an optional EmployeeName field.
  • Reversible: Follows EF Core best practices with Up/Down methods for forward and backward compatibility.
  • Next Steps:
    1. Update your domain model to include EmployeeName.
    2. Rebuild and run your application to utilize the new column.
    3. Test creating, reading, and updating MechanicTimesheet records with the new field.

πŸ‘ By following this migration pattern, your database evolves alongside your code, ensuring data integrity and application stability.


Happy coding & smooth deployments! πŸš—πŸ”§