πŸ“„ Migration: Add New Column to DocumentImage (20230210162723_AddNewColumnToDocumentImage.Designer.cs)


πŸ“‘ Table of Contents

  1. πŸš€ Overview & Purpose
  2. πŸ—‚ File Structure & Metadata
  3. πŸ” Migration Anatomy
  4. πŸ“¦ Entities & Schema Snapshot
  5. πŸ”— Relationships & Constraints
  6. βš™οΈ Dependencies & Technologies
  7. πŸƒ Using the Migration
  8. πŸ“ˆ UML Diagram Example
  9. πŸ“ Conclusion

πŸš€ Overview & Purpose

This Designer file is an auto‑generated snapshot of your EF Core model after applying the migration AddNewColumnToDocumentImage on 2023‑02‑10.
It describes the shape of every table, column, index, key and relationship in your database so that EF Core can:

  • Scaffold future migrations
  • Perform comparisons between model versions
  • Generate SQL scripts for Up() and Down() operations

Tip: You should never edit this file manuallyβ€”changes here are overwritten by EF Core tooling.


πŸ—‚ File Structure & Metadata

// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

namespace IrbyImaging.DbContext.Migrations
{
    [DbContext(typeof(IrbyContext))]
    [Migration("20230210162723_AddNewColumnToDocumentImage")]
    partial class AddNewColumnToDocumentImage
    {
        protected override void BuildTargetModel(ModelBuilder modelBuilder)
        {
            // ... Entity definitions go here ...
        }
    }
}
Section Description
<auto-generated /> Flags that this file is maintained by a tool (EF Core migration).
DbContext(typeof(...)) Associates this migration with the IrbyContext.
Migration("202302...") Unique ID & name of the migration (timestamp + descriptive name).
BuildTargetModel(...) Core method: defines the final EF model snapshot after this migration.

πŸ” Migration Anatomy

Migration Attributes

  • DbContextAttribute
    References the IrbyContextβ€”your EF Core DbContext implementation.

  • MigrationAttribute
    The string (20230210162723_AddNewColumnToDocumentImage) encodes:

    1. 20230210162723: UTC timestamp (YYYYMMDDHHMMSS)
    2. AddNewColumnToDocumentImage: Descriptive name

BuildTargetModel Method

  • Invoked by EF Core to rebuild the ModelBuilder state.
  • Chains calls like modelBuilder.Entity(...), .Property(...), .HasKey(...), .HasIndex(...), etc.
  • Captures:
    • Tables
    • Columns with CLR types & SQL mappings
    • Primary Keys, Foreign Keys, Relationships
    • Indexes and Annotations

Warning:
Regions suppressed with #pragma warning disable 612, 618 allow legacy code or deprecated APIs to remain.


πŸ“¦ Entities & Schema Snapshot

Key Entities Overview

This migration snapshot defines the following entities (with their schemas in the irby schema unless noted):

Entity Name Table Name Notes
Asset irby.Asset + Discriminator for Computer/Printer/etc.
AssetHistory irby.AssetHistory Tracks lifecycle events
AssetModel irby.AssetModel FK β†’ AssetManufacturer
DocumentImage irby.DocumentImage πŸ“Œ Target of this migration
Ticket irby.Ticket + Updates navigation
IrbyForms irby.IrbyForms Staged forms storage
CovidTestData irby.CovidTestData Custom PK name
... (many more) ... Omitted for brevity

DocumentImage Entity & New Column

modelBuilder.Entity("IrbyImaging.DomainClasses.DocumentImage", b =>
{
    b.Property<Guid>("DocumentImageId")
        .HasColumnType("uniqueidentifier");              // PK
    b.HasKey("DocumentImageId")
     .HasName("PrimaryKey_DocumentImageId");

    // ← Existing columns (FileName, ImageContents, ...)
    b.Property<byte[]>("ImageContents")
        .HasColumnType("varbinary(max)");

    // πŸ“Œ **NEW COLUMN** introduced in this migration:
    b.Property<string>("WorkOrder")
        .HasColumnType("nvarchar(max)");

    b.ToTable("DocumentImage", "irby");
});
Column Name CLR Type SQL Type Notes
DocumentImageId Guid uniqueidentifier Primary Key
FileName string nvarchar(max) Original file name
ImageContents byte[] varbinary(max) Raw image bytes
WorkOrder string nvarchar(max) ❇️ Newly added in this migration

Why add WorkOrder?
To capture a new business identifier that ties scanned documents to work orders in downstream workflows.


πŸ”— Relationships & Constraints

A few representative relationships:

  • Asset β†’ FieldOffice

    modelBuilder.Entity("Asset", b =>
      b.HasOne("FieldOffice", "FieldOffice")
       .WithMany("Assets")
       .HasForeignKey("FieldOfficeId"));
    
  • Asset β†’ AssetModel

    b.HasOne("AssetModel", "Model")
     .WithMany()
     .HasForeignKey("ModelId")
     .OnDelete(DeleteBehavior.Cascade);
    
  • Ticket β†’ Update

    modelBuilder.Entity("Update", b =>
      b.HasOne("Ticket", null)
       .WithMany("Updates")
       .HasForeignKey("TicketId")
       .OnDelete(DeleteBehavior.Cascade));
    
  • Inheritance via Discriminator
    The Asset table uses a discriminator column (Discriminator) to represent derived types:

    • Computer, Printer, MobileDevice, Dock, etc.

βš™οΈ Dependencies & Technologies

  • .NET 5.0
  • Entity Framework Core 5.0.12
  • Microsoft.EntityFrameworkCore.SqlServer
  • Microsoft.EntityFrameworkCore.Tools

These packages provide the ModelBuilder, migrations API, SQL Server provider, and design‑time tools.


πŸƒ Using the Migration

Applying & Rolling Back

  1. Add Migration (already done):
    dotnet ef migrations add AddNewColumnToDocumentImage
    
  2. Update Database (apply):
    dotnet ef database update
    
  3. Rollback (to previous):
    dotnet ef database update [PreviousMigrationName]
    

Code Snippets

  • Check the Generated SQL

    dotnet ef migrations script \
      --from 20230210162723_PreviousMigration \
      --to   20230210162723_AddNewColumnToDocumentImage
    
  • Manual SQL Example

    ALTER TABLE [irby].[DocumentImage]
    ADD [WorkOrder] NVARCHAR(MAX) NULL;
    

πŸ“ˆ UML Diagram Example

classDiagram
    class DocumentImage {
        +Guid DocumentImageId
        +string FileName
        +byte[] ImageContents
        +string ImageOCRText
        +bool IsLegalDocument
        +string WorkOrder      <<new>>
    }
    class IrbyContext {
        +DbSet<DocumentImage> DocumentImages
        +DbSet<Asset> Assets
        // ...
    }
    IrbyContext "1" -- "0..*" DocumentImage

This diagram shows DocumentImage with the WorkOrder property highlighted.


πŸ“ Conclusion

This Designer file is the heart of EF Core’s migration mechanism. It represents the complete database model as EF Core understands it after your AddNewColumnToDocumentImage migration.

  • Do not edit it manually.
  • Use EF Core tools (dotnet ef) to keep your model in sync.
  • Verify the new column (WorkOrder) appears in your database and in LINQ queries against DbContext.DocumentImages.

Happy migrating! πŸš€