πŸ“š Documentation: 20230711153123_AddCategoryColumn.cs


πŸ“‘ Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Migration Structure
  4. Detailed Code Walkthrough
  5. Schema Impact Diagram (Mermaid)
  6. Usage Examples
  7. Rolling Back the Migration
  8. Best Practices & Tips
  9. Conclusion

🧐 Overview & Purpose

File:
IrbyImaging.DbContext/Migrations/20230711153123_AddCategoryColumn.cs

This EF Core migration adds a new integer column named Category (with a default value of 0) to multiple tables in the "irby" schema:

  • TempDocumentData
  • DocumentType
  • DocumentImage
  • DocumentCategory
  • DocumentAttribute

Why?
Introducing a Category field allows you to classify or group documents, images, types, attributes, etc., under a shared numeric category for filtering, reporting, or business rules.


βš™οΈ Dependencies & Technologies

Technology Version / Notes
.NET Core / .NET 6+ Target framework for EF Core
Microsoft.EntityFrameworkCore ORM library providing Migrations API
Microsoft.EntityFrameworkCore.Tools CLI tooling for migrations
C# Language of the migration class

CLI Prerequisites:

  • dotnet ef global tool installed
  • A valid DbContext configured to use the "irby" schema

πŸ—οΈ Migration Structure

public partial class AddCategoryColumn : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder) { /* ... */ }
    protected override void Down(MigrationBuilder migrationBuilder) { /* ... */ }
}
  • AddCategoryColumn
    Partial class that inherits from Migration.
  • Up
    Defines schema changes to apply (adding columns).
  • Down
    Defines how to revert changes (dropping columns).

πŸ” Detailed Code Walkthrough

4.1 Up Method

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.AddColumn<int>(
        name: "Category",
        schema: "irby",
        table: "TempDocumentData",
        type: "int",
        nullable: false,
        defaultValue: 0);

    // Repeat for other tables...
    migrationBuilder.AddColumn<int>(
        name: "Category",
        schema: "irby",
        table: "DocumentType",
        type: "int", nullable: false, defaultValue: 0);
    migrationBuilder.AddColumn<int>(
        name: "Category",
        schema: "irby",
        table: "DocumentImage",
        type: "int", nullable: false, defaultValue: 0);
    migrationBuilder.AddColumn<int>(
        name: "Category",
        schema: "irby",
        table: "DocumentCategory",
        type: "int", nullable: false, defaultValue: 0);
    migrationBuilder.AddColumn<int>(
        name: "Category",
        schema: "irby",
        table: "DocumentAttribute",
        type: "int", nullable: false, defaultValue: 0);
}
  • AddColumn<int>
    Adds a non-nullable int column named Category.
  • defaultValue: 0
    Ensures existing rows get 0 so the migration won't fail on non-null constraint.

4.2 Down Method

protected override void Down(MigrationBuilder migrationBuilder)
{
    migrationBuilder.DropColumn(
        name: "Category",
        schema: "irby",
        table: "TempDocumentData");

    // Repeat for other tables...
    migrationBuilder.DropColumn(name: "Category", schema: "irby", table: "DocumentType");
    migrationBuilder.DropColumn(name: "Category", schema: "irby", table: "DocumentImage");
    migrationBuilder.DropColumn(name: "Category", schema: "irby", table: "DocumentCategory");
    migrationBuilder.DropColumn(name: "Category", schema: "irby", table: "DocumentAttribute");
}
  • DropColumn
    Safely removes the Category column from each table, reverting schema to its previous state.

πŸ“Š Schema Impact Diagram (Mermaid)

erDiagram
    TempDocumentData {
        int Id PK
        int Category
        /* other fields... */
    }
    DocumentType {
        int Id PK
        int Category
    }
    DocumentImage {
        int Id PK
        int Category
    }
    DocumentCategory {
        int Id PK
        int Category
    }
    DocumentAttribute {
        int Id PK
        int Category
    }

This diagram shows each table receiving a new Category attribute.


πŸ› οΈ Usage Examples

  1. Add & Apply Migration

    dotnet ef migrations add AddCategoryColumn --context IrbyDbContext
    dotnet ef database update --context IrbyDbContext
    
  2. Querying by Category

    var invoices = dbContext.DocumentType
        .Where(dt => dt.Category == 5)
        .ToList();
    
  3. Updating Category

    var docImage = await dbContext.DocumentImage.FindAsync(123);
    docImage.Category = 2;
    await dbContext.SaveChangesAsync();
    

πŸ”„ Rolling Back the Migration

To revert this migration:

dotnet ef database update <PreviousMigrationName> --context IrbyDbContext

This will invoke the Down method, dropping all Category columns.


πŸ’‘ Best Practices & Tips

  • Backup Database: Always backup before applying schema changes.
  • Seed Default Values: If you need meaningful defaults (not just 0), consider a custom UPDATE SQL in the Up method.
  • Test Locally: Run migrations on a local DB to catch errors.
  • Migration Order: Ensure no dependent code references Category before the migration is applied.

🏁 Conclusion

This migration extends your data model by adding a uniform Category column across key document-related tables. It’s a straightforward schema update that unlocks new filtering, grouping, and business logic capabilities. By following the usage examples and best practices, you can seamlessly integrate and manage this change in your EF Core workflow. πŸŽ‰


Generated by EF Core Migrations β€’ Β© IrbyImaging