📑 Documentation: 20220614182210_NewColumnDocumentImageTable2.cs
Assembly:
IrbyImaging.DbContext
Author: kvamshi
Created: 06-14-2022
Last Modified: 06-14-2022
🗂 Table of Contents
- Overview & Purpose
- Key Functional Areas
- Dependencies & Technologies
- Data Model Changes
- Migration Class Details
- Usage Examples
- Error Handling & Rollbacks
- Third‑Party Integrations
- Conclusion
🔍 Overview & Purpose
This EF Core Migration adds a new Boolean column, IsLegalDocument, to two existing tables in the irby schema:
TempDocumentDataDocumentImage
Why?
To flag whether a document in these tables is categorized as a “legal document” within the system. This supports downstream logic that may filter, process or display legal documents differently.
🔑 Key Functional Areas
- Schema Evolution: Introduces schema changes without data loss.
- Backward‑Compatible: Default value ensures existing rows are set to
false. - Reversible: Provides an explicit Down method to roll back the changes.
⚙️ Dependencies & Technologies
| Technology | Version / Details |
|---|---|
| Microsoft.EntityFrameworkCore | Used for Migrations (Migration, MigrationBuilder) |
| C# / .NET Core | Migration implemented in C# |
| SQL Server (assumed)** | Column type bit |
Note: Adjust database provider as needed; column type
bitis standard in SQL Server for Boolean values.
🏗 Data Model Changes
New Columns Introduced
| Schema | Table | Column | SQL Type | Nullable | Default Value |
|---|---|---|---|---|---|
| irby | TempDocumentData | IsLegalDocument | bit | No | false |
| irby | DocumentImage | IsLegalDocument | bit | No | false |
UML Diagram
+---------------------+
| <<Migration>> |
| NewColumnDocument… |
+---------------------+
| - Up(mb) |
| - Down(mb) |
+---------------------+
Up: Adds two columnsDown: Removes those columns
🛠 Migration Class Details
using Microsoft.EntityFrameworkCore.Migrations;
namespace IrbyImaging.DbContext.Migrations
{
public partial class NewColumnDocumentImageTable2 : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<bool>(
name: "IsLegalDocument",
schema: "irby",
table: "TempDocumentData",
type: "bit",
nullable: false,
defaultValue: false);
migrationBuilder.AddColumn<bool>(
name: "IsLegalDocument",
schema: "irby",
table: "DocumentImage",
type: "bit",
nullable: false,
defaultValue: false);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "IsLegalDocument",
schema: "irby",
table: "TempDocumentData");
migrationBuilder.DropColumn(
name: "IsLegalDocument",
schema: "irby",
table: "DocumentImage");
}
}
}
Up Method
- Purpose: Apply schema changes.
- Actions:
AddColumn<bool>("IsLegalDocument", ...)onTempDocumentDataAddColumn<bool>("IsLegalDocument", ...)onDocumentImage
- DefaultValue:
falseensures existing data remains consistent.
Down Method
- Purpose: Revert schema to previous state.
- Actions:
DropColumn("IsLegalDocument")fromTempDocumentDataDropColumn("IsLegalDocument")fromDocumentImage
🚀 Usage Examples
-
Add Migration
dotnet ef migrations add NewColumnDocumentImageTable2 \ --project IrbyImaging.DbContext \ --startup-project IrbyImaging.API -
Update Database
dotnet ef database update \ --project IrbyImaging.DbContext \ --startup-project IrbyImaging.API -
Verify Schema
SELECT COLUMN_NAME, DATA_TYPE, COLUMN_DEFAULT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'irby' AND TABLE_NAME IN ('TempDocumentData','DocumentImage');
🛡 Error Handling & Rollbacks
- Migration Errors: If adding a column fails (e.g., permission issues), the EF Core tooling will halt and report the issue.
- Rollback Strategy:
This invokes thedotnet ef database update LastGoodMigrationDownmethods of subsequent migrations automatically.
🌐 Third‑Party Integrations
- Entity Framework Core Migrations: Handles schema diffs and generates SQL.
- SQL Server: Column type
bitis SQL Server–specific; adapt for other providers if required (e.g.,booleanin PostgreSQL).
📝 Conclusion
This migration NewColumnDocumentImageTable2 elegantly extends your database model by tagging documents as “legal.” It’s:
- Non‑disruptive: Default values preserve existing records.
- Reversible: Provides a clean rollback.
- Lightweight: Two straightforward schema operations.
Apply it via dotnet ef database update, and your application can immediately begin utilizing the new IsLegalDocument flags in queries, views or business logic. 🚀
Happy Coding! 🛠️🎉