π Documentation: IrbyImaging.DbContext/Migrations/20220622145439_AddNewCategoryTable.cs
π Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Key Functional Areas
- Data Models & Schema Changes
- UML Diagram
- Usage & Examples
- Error Handling & Return Types
- API Endpoints Overview
- Helper Methods & Utilities
- Thirdβparty Integrations
- Conclusion π―
1. π Overview & Purpose
This Entity Framework Core migration introduces a new table named DocumentCategory and adds a DocumentCategory column to the existing DocumentType table within the irby schema.
- Filename:
20220622145439_AddNewCategoryTable.cs - Namespace:
IrbyImaging.DbContext.Migrations - Created/Modified: 06-22-2022 by
kvamshi
Why?
- To categorize documents in the system with richer metadata (name, description, availability flags, audit fields).
- Maintain backwards compatibility: existing
DocumentTyperecords get a new column (DocumentCategory) to link or describe categories.
2. βοΈ Dependencies & Technologies
| Technology/Package | Version/Details |
|---|---|
| .NET Core / .NET 5+ | Entity Framework Core migrations |
| Microsoft.EntityFrameworkCore | Microsoft.EntityFrameworkCore 5.x+ |
| SQL Server | uniqueidentifier, nvarchar(max), bit, datetime2 |
3. π Key Functional Areas
Up Method Operations π
protected override void Up(MigrationBuilder migrationBuilder)
{
// 1. Add 'DocumentCategory' column to 'DocumentType' table
migrationBuilder.AddColumn<string>(
name: "DocumentCategory",
schema: "irby",
table: "DocumentType",
type: "nvarchar(max)",
nullable: true);
// 2. Create new 'DocumentCategory' table
migrationBuilder.CreateTable(
name: "DocumentCategory",
schema: "irby",
columns: table => new
{
Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
Name = table.Column<string>(type: "nvarchar(max)", nullable: true),
Description = table.Column<string>(type: "nvarchar(max)", nullable: true),
IsAvailable = table.Column<bool>(type: "bit", nullable: false),
IsDeleted = table.Column<bool>(type: "bit", nullable: false),
AddedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
AddedDate = table.Column<DateTime>(type: "datetime2", nullable: false),
ModifiedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
ModifiedDate = table.Column<DateTime>(type: "datetime2", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_DocumentCategory", x => x.Id);
});
}
-
AddColumn
- Table:
irby.DocumentType - New Column:
DocumentCategory(string, nullable)
- Table:
-
CreateTable
- Table:
irby.DocumentCategory - Columns:
Id(GUID, PK)Name,Description(nvarchar)- Flags:
IsAvailable,IsDeleted - Audit:
AddedBy,AddedDate,ModifiedBy,ModifiedDate
- Table:
Down Method Operations π
protected override void Down(MigrationBuilder migrationBuilder)
{
// 1. Drop 'DocumentCategory' table
migrationBuilder.DropTable(
name: "DocumentCategory",
schema: "irby");
// 2. Remove 'DocumentCategory' column from 'DocumentType'
migrationBuilder.DropColumn(
name: "DocumentCategory",
schema: "irby",
table: "DocumentType");
}
- Rolls back all schema changes introduced in Up().
4. ποΈ Data Models & Schema Changes
DocumentCategory Table
| Column | Type | Nullable | Description |
|---|---|---|---|
| Id | uniqueidentifier | No | Primary Key |
| Name | nvarchar(max) | Yes | Category display name |
| Description | nvarchar(max) | Yes | Category details/notes |
| IsAvailable | bit (bool) | No | Indicates if category is active |
| IsDeleted | bit (bool) | No | Soft-delete flag |
| AddedBy | nvarchar(max) | Yes | User/Service that created record |
| AddedDate | datetime2 | No | Timestamp when created |
| ModifiedBy | nvarchar(max) | Yes | Last modifier |
| ModifiedDate | datetime2 | No | Timestamp when modified |
DocumentType Table Alteration
| Table | Column Name | Type | Nullable | Purpose |
|---|---|---|---|---|
irby.DocumentType |
DocumentCategory | nvarchar(max) | Yes | Link/reference to category name/ID |
5. π UML Diagram
classDiagram
class DocumentCategory {
+Guid Id
+string Name
+string Description
+bool IsAvailable
+bool IsDeleted
+string AddedBy
+DateTime AddedDate
+string ModifiedBy
+DateTime ModifiedDate
}
class DocumentType {
+Guid Id
+string TypeName
+string DocumentCategory // newly added
// ... other existing fields
}
DocumentType : DocumentCategory (string)
6. π Usage & Examples
-
Add Migration
dotnet ef migrations add AddNewCategoryTable \ --project IrbyImaging.DbContext \ --context IrbyImagingDbContext -
Apply Migration
dotnet ef database update \ --project IrbyImaging.DbContext \ --context IrbyImagingDbContext -
Inspect Database
- Confirm
irby.DocumentCategorytable is created. - Confirm
DocumentType.DocumentCategorycolumn exists.
- Confirm
7. π οΈ Error Handling & Return Types
- Migrations do not return values or throw exceptions at build-time.
- Runtime errors may occur if the target schema doesnβt exist or permissions are insufficient.
- Ensure Idempotency by running
Down()before re-applying migration if needed.
8. π API Endpoints Overview
Not applicable: This file is strictly a database migration. There are no direct API endpoints.
9. π§© Helper Methods & Utilities
Not applicable: All logic is contained within the
Up()andDown()override methods of the EF CoreMigrationbase class.
10. π Thirdβparty Integrations
- Entity Framework Core: uses built-in
MigrationBuilder. - No external NuGet packages beyond EF Core.
11. π― Conclusion
This migration cleanly extends the existing database schema to support document categorization.
- Forward migration adds a new table and column.
- Rollback removes them, preserving backward compatibility.
By following the Usage & Examples, teams can seamlessly integrate this change into CI/CD pipelines, ensuring data consistency across environments. π
Happy coding & happy categorizing! π