π Documentation: 20220803204201_UpdateAttributeList.cs
Location:
IrbyImaging.DbContext/Migrations/20220803204201_UpdateAttributeList.cs
ποΈ Table of Contents
- π Overview & Purpose
- π§ Dependencies & Technologies
- π― Key Functional Areas
- ποΈ Data Model Changes
- π οΈ
Up()Method Details - π‘οΈ
Down()Method Details - βοΈ Usage & Examples
- π Migration Workflow
- πΌοΈ UML Diagram
- π Conclusion
π Overview & Purpose
This Entity Framework Core migration introduces three new boolean columns to the irby.DocumentAttribute table:
- IsRequired
- LinkEmployeeData
- LinkJobData
These flags allow the application to dynamically determine:
- β Whether a document attribute is mandatory.
- π Whether it should link to employee-level data.
- π Whether it should link to job-level data.
Why?
- Enforce attribute-level validation (
IsRequired).- Support dynamic data binding scenarios (
LinkEmployeeData,LinkJobData).
π§ Dependencies & Technologies
| Technology | Version / Detail |
|---|---|
| .NET Core | 6.0+ |
| Entity Framework | EF Core Migrations (Microsoft.EntityFrameworkCore.Migrations) |
| C# | 10.0+ |
| Database Provider | SQL Server (via bit type) |
π― Key Functional Areas
- Schema Evolution:
Modifies the schema of an existing table without data loss. - Version Control:
Captures changes in a timestamped migration file (20220803204201). - Rollβforward / Rollβback:
ProvidesUp()to apply changes,Down()to revert them.
ποΈ Data Model Changes
Before Migration
| Column Name | Data Type | Nullable | Purpose |
|---|---|---|---|
| Id | int | no | Primary Key |
| Name | nvarchar | no | Attribute name |
| Value | nvarchar | yes | Attribute value |
After Migration
| Column Name | Data Type | Nullable | Default | Description |
|---|---|---|---|---|
| IsRequired | bit |
no | false |
Marks attribute mandatory |
| LinkEmployeeData | bit |
no | false |
Indicates binding to employee-specific data |
| LinkJobData | bit |
no | false |
Indicates binding to job-specific data |
π οΈ Up() Method Details
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<bool>(
name: "IsRequired",
schema: "irby",
table: "DocumentAttribute",
type: "bit",
nullable: false,
defaultValue: false);
migrationBuilder.AddColumn<bool>(
name: "LinkEmployeeData",
schema: "irby",
table: "DocumentAttribute",
type: "bit",
nullable: false,
defaultValue: false);
migrationBuilder.AddColumn<bool>(
name: "LinkJobData",
schema: "irby",
table: "DocumentAttribute",
type: "bit",
nullable: false,
defaultValue: false);
}
-
AddColumn<bool>
- schema:
"irby" - table:
"DocumentAttribute" - type:
bit - nullable:
false - defaultValue:
false
- schema:
-
Behavior:
- Idempotent Addition: Running
Up()multiple times in the same database context will throw if columns already exist. - Default Values: Ensures existing rows get a value of
false.
- Idempotent Addition: Running
π‘οΈ Down() Method Details
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "IsRequired",
schema: "irby",
table: "DocumentAttribute");
migrationBuilder.DropColumn(
name: "LinkEmployeeData",
schema: "irby",
table: "DocumentAttribute");
migrationBuilder.DropColumn(
name: "LinkJobData",
schema: "irby",
table: "DocumentAttribute");
}
- Purpose: Reverts the schema to its prior state by removing the three columns.
- Caution: Data stored in these columns will be lost on rollback.
βοΈ Usage & Examples
- Add Migration
dotnet ef migrations add UpdateAttributeList - Apply Migration
dotnet ef database update - Inspect Database
SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'irby' AND TABLE_NAME = 'DocumentAttribute';
π Migration Workflow
flowchart LR
A[Start: CLI/CodeChange] --> B[Generate Migration]
B --> C{Migration file created?}
C -- Yes --> D[Review & Customize]
D --> E[dotnet ef database update]
E --> F[Schema updated]
F --> G[App uses new columns!]
C -- No --> H[Error: Migration not found]
πΌοΈ UML Diagram
classDiagram
class DocumentAttribute {
+int Id
+string Name
+string Value
+bool IsRequired
+bool LinkEmployeeData
+bool LinkJobData
}
- Note: Only the new attributes are shown in bold in the diagram.
π Conclusion
This migration is a lightweight yet powerful schema update that enhances the flexibility of document attributes:
- βοΈ Enables mandatory flags via
IsRequired. - βοΈ Provides linking options for employee and job data.
- βοΈ Maintains backward compatibility with default
falsevalues.
Next Steps:
- Update your applicationβs data access layer (e.g., EF Core models, validation logic) to leverage the new flags.
- Add UI checkboxes or API metadata to expose these behaviors at runtime.
- Write unit/integration tests to cover new business rules around required and linked data.
π Happy Migrating! π