π Documentation: 20220809153958_UpdateDocImageTable.cs
π Table of Contents
- Overview & Purpose
- Key Functional Areas
- Dependencies & Technologies
- Data Model Changes
- Migration Class Structure
- Usage Instructions
- Code Snippets & Examples
- UML Diagram
- Conclusion
π 1. Overview & Purpose
The UpdateDocImageTable migration modifies the database schema of the DocumentImage table by adding a new column:
- LegalDocumentType (
uniqueidentifier, non-nullable)
This migration ensures that each record within the irby.DocumentImage table can reference a legal document type (via a GUID) within your system.
Why is it needed?
- Track different categories/types of legal documents associated with an image.
- Enforce data integrity at the database level by preventing null values for
LegalDocumentType.
π 2. Key Functional Areas
| Area | Description |
|---|---|
| Schema Evolution | Adds a new column to an existing table without data loss. |
| Data Integrity | Ensures LegalDocumentType is always populated (non-nullable, default GUID = zero). |
| Rollback Capability | Provides a Down method to remove the column if needed. |
| EF Core Migrations Workflow | Integrates into EF Coreβs migration pipeline (dotnet ef). |
π οΈ 3. Dependencies & Technologies
- .NET Core / .NET 5+
- Entity Framework Core (EF Core)
Microsoft.EntityFrameworkCore.Migrations
- SQL Server (implied by
uniqueidentifiertype) - C# Language Features
- Generics, partial classes, override methods
Ensure your project has the EF Core tools installed:
dotnet tool install --global dotnet-ef
dotnet add package Microsoft.EntityFrameworkCore.Design
ποΈ 4. Data Model Changes
Before this migration, the DocumentImage table had columns such as:
| Column Name | Data Type | Nullable |
|---|---|---|
| Id | uniqueidentifier | No |
| DocumentId | uniqueidentifier | No |
| ImagePath | nvarchar(max) | Yes |
| CreatedAt | datetime2 | No |
| ... | ... | ... |
After applying this migration, the table gains:
| Column Name | Data Type | Nullable | Default Value |
|---|---|---|---|
| LegalDocumentType | uniqueidentifier | No | 00000000-0000-0000-0000-000000000000 |
π§ 5. Migration Class Structure
namespace IrbyImaging.DbContext.Migrations
{
public partial class UpdateDocImageTable : Migration
{
protected override void Up(MigrationBuilder migrationBuilder) { /* ... */ }
protected override void Down(MigrationBuilder migrationBuilder) { /* ... */ }
}
}
π Up Method
- Purpose: Apply the schema change.
- Operation:
migrationBuilder.AddColumn<Guid>( name: "LegalDocumentType", schema: "irby", table: "DocumentImage", type: "uniqueidentifier", nullable: false, defaultValue: new Guid("00000000-0000-0000-0000-000000000000")); - Effects:
- Adds
LegalDocumentTypeas a non-nullable GUID. - Sets default to the βemptyβ GUID for all existing rows.
- Adds
π½ Down Method
- Purpose: Roll back the schema change.
- Operation:
migrationBuilder.DropColumn( name: "LegalDocumentType", schema: "irby", table: "DocumentImage"); - Effects:
- Removes the
LegalDocumentTypecolumn entirely.
- Removes the
π 6. Usage Instructions
Applying the Migration
- Create Migration (already done):
dotnet ef migrations add UpdateDocImageTable - Apply to Database:
dotnet ef database update - Verify:
SELECT TOP 1 LegalDocumentType FROM irby.DocumentImage;
Reverting the Migration
- Rollback One Step:
dotnet ef database update <PreviousMigrationName> - Remove Migration Files (if needed):
dotnet ef migrations remove
π‘ 7. Code Snippets & Examples
// Adding a DocumentImage with a LegalDocumentType
var docImage = new DocumentImage
{
Id = Guid.NewGuid(),
DocumentId = someDocumentId,
ImagePath = "path/to/image.png",
CreatedAt = DateTime.UtcNow,
LegalDocumentType = new Guid("11111111-2222-3333-4444-555555555555")
};
_dbContext.DocumentImages.Add(docImage);
_dbContext.SaveChanges();
-- Query to find all images of a specific legal document type
SELECT *
FROM irby.DocumentImage
WHERE LegalDocumentType = '11111111-2222-3333-4444-555555555555';
π 8. UML Diagram
@startuml
class Migration {
+ Up(MigrationBuilder)
+ Down(MigrationBuilder)
}
class UpdateDocImageTable {
+ Up(MigrationBuilder)
+ Down(MigrationBuilder)
}
Migration <|-- UpdateDocImageTable
@enduml
β 9. Conclusion
This migration enhances the DocumentImage table by introducing a critical column, LegalDocumentType, ensuring each image is properly categorized by its legal document type. The built-in Up/Down methods provide seamless schema evolution and rollback capabilities using EF Coreβs migration framework. Apply it to keep your database in sync with your domain model!
Generated with β€οΈ by your Documentation Assistant