πŸ“‚ IrbyImaging.DbContext Β· Migration: MoreUpdatesToJobTable


πŸ“– Table of Contents

  1. Overview & Purpose
  2. Key Functional Areas
  3. Dependencies & Technologies
  4. Data Model: Job Table Before & After
  5. Migration Class Structure
  6. Up Method (Applying the Migration) πŸ†™
  7. Down Method (Reverting the Migration) πŸ”„
  8. UML Diagram πŸ“Š
  9. Usage Examples
  10. Conclusion

1. Overview & Purpose

This migration fileβ€”20240130134140_MoreUpdatesToJobTable.csβ€”is part of the IrbyImaging.DbContext project and uses Entity Framework Core Migrations to alter the schema of the irby.Job table.

Primary goal:

  • Add a new nullable CustomerId column of type uniqueidentifier to link each Job row optionally to a Customer.
  • Provide a rollback path by removing CustomerId in the Down() method.

βš™οΈ This change enables the application to associate imaging jobs with customers, supporting future features like customer-specific job queries.


2. Key Functional Areas

  • Schema Evolution
    Leverage EF Core Migrations to update the database without manual SQL scripts.

  • Column Management

    • AddColumn in Up()
    • DropColumn in Down()
  • Version Control of Database
    Track changes in code, maintain history, and enable rolling back if needed.


3. Dependencies & Technologies

Type Technology / Package
Programming Language C# (.NET 7+)
ORM / Migrations Framework Microsoft.EntityFrameworkCore
Database SQL Server (assumed from uniqueidentifier)
Namespaces System, Microsoft.EntityFrameworkCore.Migrations

4. Data Model: Job Table Before & After

Before Migration

Column Name Data Type Nullable Description
Id uniqueidentifier False Primary Key
Name nvarchar(200) False Job name
CreatedDate datetime2 False When the job was created
(other columns) … … …

After Migration

Column Name Data Type Nullable Description
CustomerId uniqueidentifier True Optional FK to Customer.Id

5. Migration Class Structure

using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

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

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            // Remove CustomerId column
        }
    }
}
  • partial class MoreUpdatesToJobTable : Migration
    Inherits from EF Core’s Migration base class.
  • Up()
    Applies schema changes.
  • Down()
    Reverts schema changes.

6. Up Method (Applying the Migration) πŸ†™

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.AddColumn<Guid>(
        name: "CustomerId",
        schema: "irby",
        table: "Job",
        type: "uniqueidentifier",
        nullable: true);
}

What it does:

  1. AddColumn<Guid>
    • name: "CustomerId" – Column name.
    • schema: "irby" – Database schema.
    • table: "Job" – Target table.
    • type: "uniqueidentifier" – SQL Server GUID type.
    • nullable: true – Allows nulls (optional relationship).

πŸš€ Result: The Job table now has a CustomerId column, ready to store GUIDs.


7. Down Method (Reverting the Migration) πŸ”„

protected override void Down(MigrationBuilder migrationBuilder)
{
    migrationBuilder.DropColumn(
        name: "CustomerId",
        schema: "irby",
        table: "Job");
}

What it does:

  1. DropColumn – Completely removes CustomerId from irby.Job.

⚠️ Caution: Dropping the column deletes all CustomerId data in existing rows.


8. UML Diagram πŸ“Š

+------------------------------------+
| << EFCore Migration >>             |
| MoreUpdatesToJobTable              |
+------------------------------------+
| + Up(MigrationBuilder) : void     |
| + Down(MigrationBuilder) : void   |
+------------------------------------+
           β–²
           |
+---------------------------+
| Microsoft.EntityFrameworkCore.Migrations.Migration |
+---------------------------+

9. Usage Examples

9.1. Adding & Applying the Migration

# 1. Scaffold the migration
dotnet ef migrations add MoreUpdatesToJobTable \
    --context IrbyDbContext \
    --output-dir Migrations

# 2. Update the database
dotnet ef database update

9.2. Rolling Back (Reverting)

# Revert to the previous migration
dotnet ef database update <PreviousMigrationName>

9.3. Verifying the Change

-- In SQL Server Management Studio or Azure Data Studio
SELECT TOP 1 CustomerId FROM irby.Job;

10. Conclusion

This MoreUpdatesToJobTable migration is a focused, reversible database schema update that introduces a nullable CustomerId to the Job table. It paves the way for linking jobs to customers in your imaging workflow, all while maintaining a clear migration history. Applying and rolling back is straightforward via standard EF Core CLI commands, making your database evolution predictable and versioned.

Enjoy building customer-centric features! πŸŽ‰


Generated by IrbyImaging.DbContext Migration Documentation Generator