πŸ“š Documentation: 20230201064404_LyTxTableAddColumn.cs


πŸ“‘ Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Key Functional Areas
  4. Data Model Impact
  5. Migration Workflow
  6. Usage Examples
  7. UML Diagram
  8. Error Handling & Rollbacks
  9. Conclusion

🧐 1. Overview & Purpose

This file is an Entity Framework Core Migration that adds a new non-nullable BatchId column (of type Guid) to the LytxTemplate table in the irby schema.
Migrations allow you to evolve your database schema over time in a consistent, version-controlled way.

Filename:

IrbyImaging.DbContext/Migrations/20230201064404_LyTxTableAddColumn.cs

Primary Goals:

  • Introduce a BatchId column to group or batch process LytxTemplate records.
  • Ensure existing rows have a default GUID (00000000-0000-0000-0000-000000000000) to maintain non-null constraint.

πŸ› οΈ 2. Dependencies & Technologies

Technology/Package Purpose
Microsoft.EntityFrameworkCore Core EF functionality & base Migration class
Microsoft.EntityFrameworkCore.Design Design-time tools for migrations (e.g., CLI commands)
Microsoft.EntityFrameworkCore.Relational Relational database extensions (schemas, type mapping, etc.)
.NET 6+ / .NET Core Target framework for IrbyImaging project
SQL Server (assumed) Provides support for uniqueidentifier column type

βš™οΈ 3. Key Functional Areas

The migration class LyTxTableAddColumn inherits from Migration and implements two lifecycle methods:

Up Method

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.AddColumn<Guid>(
        name: "BatchId",
        schema: "irby",
        table: "LytxTemplate",
        type: "uniqueidentifier",
        nullable: false,
        defaultValue: new Guid("00000000-0000-0000-0000-000000000000")
    );
}
  • Adds a new column:
    • Name: BatchId
    • Type: uniqueidentifier (maps to Guid in C#)
    • Schema: irby
    • Non-nullable (nullable: false)
    • Default Value: All existing rows get Guid.Empty

Down Method

protected override void Down(MigrationBuilder migrationBuilder)
{
    migrationBuilder.DropColumn(
        name: "BatchId",
        schema: "irby",
        table: "LytxTemplate"
    );
}
  • Removes the BatchId column if you roll back (downgrade) this migration.

πŸ—ƒοΈ 4. Data Model Impact

Before Migration:

irby.LytxTemplate
-------------------
| Id (PK)      |
| Name         |
| CreatedDate  |
| ...          |

After Migration:

irby.LytxTemplate
-------------------
| Id (PK)      |
| Name         |
| CreatedDate  |
| ...          |
| BatchId      |  <-- new non-null Guid
Column Type Nullable Default Value
BatchId uniqueidentifier No 00000000-0000-0000-0000-000000000000

πŸš€ 5. Migration Workflow

  1. Add Migration
    dotnet ef migrations add LyTxTableAddColumn \
        --project IrbyImaging.DbContext \
        --context IrbyContext
    
  2. Update Database
    dotnet ef database update \
        --project IrbyImaging.DbContext \
        --context IrbyContext
    
  3. Verify
    • Inspect the irby.LytxTemplate table to confirm the presence of the BatchId column.
    • Check that existing rows have 00000000-0000-0000-0000-000000000000.

πŸ’» 6. Usage Examples

Querying by BatchId

var batchTemplates = await context.LytxTemplate
    .Where(t => t.BatchId == someBatchGuid)
    .ToListAsync();

Assigning a New BatchId

var newTemplate = new LytxTemplate
{
    Name = "New Report",
    CreatedDate = DateTime.UtcNow,
    BatchId = Guid.NewGuid()
};

context.LytxTemplate.Add(newTemplate);
await context.SaveChangesAsync();

πŸ“ 7. UML Diagram

classDiagram
    class Migration {
        <<abstract>>
        +Up(MigrationBuilder)
        +Down(MigrationBuilder)
    }

    class LyTxTableAddColumn {
        +Up(MigrationBuilder)
        +Down(MigrationBuilder)
    }

    Migration <|-- LyTxTableAddColumn
    LyTxTableAddColumn --> MigrationBuilder : uses

⚠️ 8. Error Handling & Rollbacks

  • Adding a Non-Nullable Column:
    • Must supply a defaultValue to avoid SQL errors when the target table already contains rows.
  • Migration Failures:
    • If database update fails, run:
      dotnet ef database update LastGoodMigrationName
      
  • Down Migration:
    • Safely removes the column.
    • Data Loss Warning: All values in BatchId will be droppedβ€”back up if needed.

🏁 9. Conclusion

This migration is a simple yet crucial step to support batch processing of LytxTemplate entities by adding a BatchId column. It demonstrates standard EF Core migration practices:

  • Version-controlled schema changes
  • Up/Down methods for applying and rolling back changes
  • Non-nullable column addition with default values

With this in place, the application can now group, filter, and batch-process templates efficiently. πŸŽ‰


πŸ“Œ See Also

Happy coding! πŸš€