π IrbyImaging.DbContext Migration
File: 20220613155005_NewColumnDocumentImageTable.cs
π Table of Contents
- Overview & Purpose
- Context: EF Core Migrations
- File Structure & Metadata
- Key Functional Areas
- Schema Changes (Up & Down)
- Dependencies & Technologies
- Code Walkthrough
7.1. Up Method π
7.2. Down Method π - UML Diagram π
- Usage Examples & Commands
- Best Practices & Recommendations
- Conclusion
1. π Overview & Purpose
This migration adds a new column, DocumentIndexes, of type nvarchar(max) to the DocumentImage table in the irby schema.
- Why?
- To store additional indexing or tagging information about document images.
- Enables flexible, text-based indexes for search or categorization features in the application.
2. π Context: EF Core Migrations
Entity Framework Core (EF Core) Migrations provide a way to incrementally evolve your database schema alongside your applicationβs data model. Each migration class:
- Derives from
Microsoft.EntityFrameworkCore.Migrations.Migration - Implements two methods:
Up(...)β apply changesDown(...)β revert changes
3. π File Structure & Metadata
| Section | Details |
|---|---|
| Assembly | IrbyImaging.DbContext |
| Author | kvamshi |
| Created On | 06-13-2022 |
| Last Modified On | 06-13-2022 |
| Namespace | IrbyImaging.DbContext.Migrations |
| Class | NewColumnDocumentImageTable |
| Inherits | Migration |
// Assembly : IrbyImaging.DbContext
// Author : kvamshi
// Created : 06-13-2022
// Last Modified : 06-13-2022
4. π© Key Functional Areas
- Schema Modification
- Add Column
DocumentIndexes - Drop Column
DocumentIndexes
- Add Column
- Version Control
- This migration has a timestamp prefix:
20220613155005_
- This migration has a timestamp prefix:
- Rollback Capability
- Provides a
Downmethod for safe reverting
- Provides a
5. π Schema Changes (Up & Down)
| Method | Operation | Schema | Table | Column | Type | Nullable |
|---|---|---|---|---|---|---|
| Up | AddColumn | irby | DocumentImage | DocumentIndexes | nvarchar(max) | Yes |
| Down | DropColumn | irby | DocumentImage | DocumentIndexes | β | β |
6. π Dependencies & Technologies
- .NET / C#
- Entity Framework Core
Microsoft.EntityFrameworkCore.Migrations
- Database
- SQL Server (
nvarchar(max))
- SQL Server (
- Development Tools
dotnet efCLI for migration commands
7. π Code Walkthrough
using Microsoft.EntityFrameworkCore.Migrations;
namespace IrbyImaging.DbContext.Migrations
{
/// <summary>
/// Adds a new column 'DocumentIndexes' to DocumentImage table.
/// </summary>
public partial class NewColumnDocumentImageTable : Migration
{
/// <summary>
/// Apply schema changes (Up).
/// </summary>
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "DocumentIndexes",
schema: "irby",
table: "DocumentImage",
type: "nvarchar(max)",
nullable: true);
}
/// <summary>
/// Revert schema changes (Down).
/// </summary>
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "DocumentIndexes",
schema: "irby",
table: "DocumentImage");
}
}
}
7.1 Up Method π
- Purpose:
Creates a new nullable columnDocumentIndexeson theirby.DocumentImagetable. - Key API Used:
migrationBuilder.AddColumn<string>( name: "DocumentIndexes", schema: "irby", table: "DocumentImage", type: "nvarchar(max)", nullable: true); - Result:
Updates database DDL to include the new column.
7.2 Down Method π
- Purpose:
Safely removes theDocumentIndexescolumn if rolling back. - Key API Used:
migrationBuilder.DropColumn( name: "DocumentIndexes", schema: "irby", table: "DocumentImage"); - Result:
Database schema reverts to its prior state.
8. π UML Diagram
@startuml
package IrbyImaging.DbContext.Migrations {
class NewColumnDocumentImageTable {
+Up(MigrationBuilder)
+Down(MigrationBuilder)
}
class Migration {
<<abstract>>
+Up(MigrationBuilder)
+Down(MigrationBuilder)
}
NewColumnDocumentImageTable --|> Migration
}
@enduml
9. π Usage Examples & Commands
- Add Migration
dotnet ef migrations add NewColumnDocumentImageTable - Update Database
dotnet ef database update - Rollback (to previous migration)
dotnet ef database update <PreviousMigrationName>
10. π Best Practices & Recommendations
- Naming Conventions
- Prefix with timestamp:
YYYYMMDDHHMMSS_DescriptiveName
- Prefix with timestamp:
- Idempotency
- Always implement both
UpandDownmethods.
- Always implement both
- Testing Migrations
- Test on a staging or local dev database before production.
- Schema Validation
- Use EF Coreβs
EnsureCreatedorEnsureDeletedfor integration tests.
- Use EF Coreβs
11. β Conclusion
This migration seamlessly extends the DocumentImage table to support flexible, text-based indexes via the new DocumentIndexes column. With clear Up and Down implementations, you can evolve your schema safely and roll back if needed.
Feel free to integrate, test, and apply this migration in your CI/CD pipeline. Happy coding! π