📋 Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Key Functional Areas
- Database Schema Changes
- UML Class Diagram
- Data Model Impact
- Usage & Examples
- Error Handling & Rollback
- Conclusion
Overview & Purpose
🔎 File:
IrbyImaging.DbContext/Migrations/20221102194233_AddNewColumnDocumentImageTable.cs
This Entity Framework Core Migration adds two new columns to the irby.DocumentImage table:
- CustomerName (nvarchar(max), nullable)
- CustomerNumber (nvarchar(max), nullable)
Why?
Storing customer metadata alongside each scanned document enables downstream reporting, filtering and improves traceability.
Dependencies & Technologies
| Dependency | Version / Details |
|---|---|
| Microsoft.EntityFrameworkCore | EF Core Migrations API |
| .NET / C# | .NET 6.0+ |
| SQL Server | nvarchar(max) column type |
- EF Core Migrations: Automates schema changes
- .NET CLI (
dotnet ef): Apply/Rollback migrations
Key Functional Areas
- Schema Modification
- Up: Add new columns
- Down: Remove added columns
- Versioning
- Timestamped class name:
20221102194233_AddNewColumnDocumentImageTable
- Timestamped class name:
- Namespacing & Scoping
- Schema:
irby - Table:
DocumentImage
- Schema:
Database Schema Changes
Up Migration
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "CustomerName",
schema: "irby",
table: "DocumentImage",
type: "nvarchar(max)",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "CustomerNumber",
schema: "irby",
table: "DocumentImage",
type: "nvarchar(max)",
nullable: true);
}
- AddColumn<string>
- name: Column name
- schema:
irby - table:
DocumentImage - type:
nvarchar(max) - nullable: true
| Column Name | Data Type | Nullable | Description |
|---|---|---|---|
| CustomerName | nvarchar(max) | Yes | Full name of the customer |
| CustomerNumber | nvarchar(max) | Yes | Unique customer ID |
Down Migration
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "CustomerName",
schema: "irby",
table: "DocumentImage");
migrationBuilder.DropColumn(
name: "CustomerNumber",
schema: "irby",
table: "DocumentImage");
}
- DropColumn
- Reverses the Up action
UML Class Diagram
classDiagram
class AddNewColumnDocumentImageTable {
<<Migration>>
+Up(MigrationBuilder)
+Down(MigrationBuilder)
}
Migration <|-- AddNewColumnDocumentImageTable
- AddNewColumnDocumentImageTable inherits from Migration
Data Model Impact
📦 DocumentImage Entity (post-migration):
public class DocumentImage
{
public int Id { get; set; }
public byte[] ImageData { get; set; }
public DateTime CreatedAt { get; set; }
// NEW properties added by this migration:
public string CustomerName { get; set; }
public string CustomerNumber { get; set; }
}
These two properties will now be mapped to additional columns in your database.
Usage & Examples
Applying the Migration
- Add Migration (already created):
dotnet ef migrations add AddNewColumnDocumentImageTable - Update Database:
dotnet ef database update- This applies the Up method, altering your schema.
Rolling Back
To revert this migration:
dotnet ef database update 20221102194232_PreviousMigrationName
or
dotnet ef database update 0
- The Down method will execute, dropping the two new columns.
Error Handling & Rollback
🚨 Potential Issues
- Existing Data: No default values are set. All existing rows get
NULLfor the new columns. - Queries: Ensure any raw SQL or stored procedures account for the new columns or ignore them.
✅ Rollback Strategy
- Use
dotnet ef database update <previousMigration>to revert schema - Always backup your database before applying/rolling back migrations
Conclusion
🎉 This migration enhances the DocumentImage table by adding rich customer metadata. By following the CLI examples above, you can seamlessly apply or revert this schema change.
Next Steps
- Update your DocumentImage entity class to include these properties.
- Modify your data access or UI layers to read/write
CustomerNameandCustomerNumber.
End of documentation for
IrbyImaging.DbContext/Migrations/20221102194233_AddNewColumnDocumentImageTable.cs 🏁