πŸ“š Documentation: 20220803204201_UpdateAttributeList.cs

Location:
IrbyImaging.DbContext/Migrations/20220803204201_UpdateAttributeList.cs


πŸ—‚οΈ Table of Contents

  1. πŸš€ Overview & Purpose
  2. πŸ”§ Dependencies & Technologies
  3. 🎯 Key Functional Areas
  4. πŸ—„οΈ Data Model Changes
  5. πŸ› οΈ Up() Method Details
  6. πŸ›‘οΈ Down() Method Details
  7. βš™οΈ Usage & Examples
  8. πŸ“ˆ Migration Workflow
  9. πŸ–ΌοΈ UML Diagram
  10. πŸ“ Conclusion

πŸš€ Overview & Purpose

This Entity Framework Core migration introduces three new boolean columns to the irby.DocumentAttribute table:

  1. IsRequired
  2. LinkEmployeeData
  3. 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:
    Provides Up() 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);
}
  1. AddColumn<bool>

    • schema: "irby"
    • table: "DocumentAttribute"
    • type: bit
    • nullable: false
    • defaultValue: false
  2. 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.

πŸ›‘οΈ 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

  1. Add Migration
    dotnet ef migrations add UpdateAttributeList
    
  2. Apply Migration
    dotnet ef database update
    
  3. 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 false values.

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! πŸŽ‰