π Documentation: 20230201064404_LyTxTableAddColumn.cs
π Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Key Functional Areas
- Data Model Impact
- Migration Workflow
- Usage Examples
- UML Diagram
- Error Handling & Rollbacks
- 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
BatchIdcolumn to group or batch processLytxTemplaterecords. - 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 toGuidin C#) - Schema:
irby - Non-nullable (
nullable: false) - Default Value: All existing rows get
Guid.Empty
- Name:
Down Method
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "BatchId",
schema: "irby",
table: "LytxTemplate"
);
}
- Removes the
BatchIdcolumn 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
- Add Migration
dotnet ef migrations add LyTxTableAddColumn \ --project IrbyImaging.DbContext \ --context IrbyContext - Update Database
dotnet ef database update \ --project IrbyImaging.DbContext \ --context IrbyContext - Verify
- Inspect the
irby.LytxTemplatetable to confirm the presence of theBatchIdcolumn. - Check that existing rows have
00000000-0000-0000-0000-000000000000.
- Inspect the
π» 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
defaultValueto avoid SQL errors when the target table already contains rows.
- Must supply a
- Migration Failures:
- If
database updatefails, run:dotnet ef database update LastGoodMigrationName
- If
- Down Migration:
- Safely removes the column.
- Data Loss Warning: All values in
BatchIdwill 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! π