📚 Documentation: 20220520152658_AddFormsTable.Designer.cs


🗂️ Table of Contents

  1. Overview & Purpose
  2. EF Core Migrations Primer
  3. Migration Class Structure
  4. BuildTargetModel Method
  5. Key Data Models & Entities
  6. Entity Relationships
  7. Usage & Code Examples
  8. Mermaid UML Diagram
  9. Applying & Reverting the Migration
  10. Conclusion

1. Overview & Purpose

🔎 File:
IrbyImaging.DbContext/Migrations/20220520152658_AddFormsTable.Designer.cs

Purpose:
This designer file is an Entity Framework Core snapshot of your database model at the point of the AddFormsTable migration. It captures:

  • The complete model (all tables & relationships).
  • The new IrbyForms table introduced in this migration.
  • Metadata annotations (SQL Server identity, column types, constraints).

Why It Matters
EF Core uses this snapshot to compute the differences between your model and the target database when you add/revert migrations.


2. EF Core Migrations Primer

  • Migrations: Version-controlled transformations of your database schema.
  • Designer Files: Auto-generated C# classes storing the model snapshot.
  • Up/Down Methods: Apply or remove schema changes.
  • BuildTargetModel: Reconstructs the current model in code.

3. Migration Class Structure

[DbContext(typeof(IrbyContext))]
[Migration("20220520152658_AddFormsTable")]
partial class AddFormsTable : Migration
{
    protected override void BuildTargetModel(ModelBuilder modelBuilder)
    {
        // Fluent API calls to configure entities...
    }
}
  • Attributes
    • [DbContext]: Links to your IrbyContext.
    • [Migration("...")]: Timestamp + descriptive name.
  • Partial Class: Complements the main migration file (Up/Down methods).

4. BuildTargetModel Method

🔧 What It Does:

  1. Defines all entities (tables), columns, keys, indexes.
  2. Annotates database provider details (e.g., SQL Server Identity).
  3. Establishes relationships via Fluent API.

Note: Although your migration specifically adds IrbyForms, the model snapshot lists every entity in your domain.


5. Key Data Models & Entities

5.1 IrbyForms Table (New) 🆕

Column Name CLR Type SQL Type Constraints
FormId Guid uniqueidentifier Primary Key
DocumentUploadedBy string nvarchar(max)
DocumentUploadedDate DateTime datetime2
FormName string nvarchar(max)
FormNumber string nvarchar(max)
ImageContents byte[] varbinary(max)

📝 Description:
The IrbyForms table stores uploaded form metadata & binary image contents for scanned paper forms.

5.2 Other Domain Entities (Snapshot)

Below is a brief summary. The full model includes ~40 entities:

Category Example Entities Notes
Assets Asset, Computer, MobileDevice, Printer, Monitor, … TPH inheritance via Discriminator column.
Billing BillingInvoice, CarrierType, GeneralLedger, GLTypeDetail Financial & invoicing tables.
Document Mgmt DocumentImage, IPMMigration, TempDocumentData, CovidTestData OCR & archival data.
HR & Jobs Employee, FieldOffice, Job, ODS_Employee_Staging, ODS_Jobs_Staging Onboarding, staging from ODS.
Support Ticket, Update, Issue Helpdesk ticketing system.
Utilities RequestOption, OperatingSystem, Manufacturer, … Lookup/reference tables.

6. Entity Relationships

🔗 Highlights:

  • Asset → FieldOffice, Asset → Employee (Owner), Asset → AssetModel
  • AssetModel → Manufacturer
  • Employee → Job
  • Ticket → Update
  • Computer/SafetyEquipment → OperatingSystem

Example Fluent API snippet:

modelBuilder.Entity("IrbyImaging.DomainClasses.Asset", b => {
    b.HasKey("Id");
    b.HasOne("IrbyImaging.DomainClasses.Employee", "Owner")
      .WithMany("Assets")
      .HasForeignKey("OwnerId");
    // ...
});

7. Usage & Code Examples

7.1 Adding the Migration

# From project root
dotnet ef migrations add AddFormsTable \
  --context IrbyContext \
  --output-dir Migrations

7.2 Applying to Database

# Updates your database to the latest migration (applies BuildTargetModel)
dotnet ef database update --context IrbyContext

7.3 Inspecting Generated SQL

dotnet ef migrations script \
  --from 20220520152657_PreviousMigration \
  --to 20220520152658_AddFormsTable

8. Mermaid UML Diagram

erDiagram
    IRBYFORMS {
        GUID FormId PK
        string FormName
        string FormNumber
        datetime DocumentUploadedDate
        string DocumentUploadedBy
        binary ImageContents
    }
    ASSET ||--o{ ASSETMODEL : belongs_to
    ASSET ||--o{ FIELD_OFFICE : located_at
    EMPLOYEE ||--o{ ASSET : owns
    TICKET ||--o{ UPDATE : has

🌐 Tip: You can paste the above in VSCode or a Mermaid live editor to visualize the schema.


9. Applying & Reverting the Migration

Action Command
Apply all pending migrations dotnet ef database update --context IrbyContext
Revert one migration dotnet ef database update 20220520152657_PreviousMigration
Remove last migration dotnet ef migrations remove --context IrbyContext

10. Conclusion

🎉 You’ve learned:

  • The role of Designer.cs in EF Core migrations.
  • How BuildTargetModel represents your complete domain at the migration point.
  • The new IrbyForms table schema & its columns.
  • Commands to add, apply, and revert migrations.

Next Steps:

  1. Review the main migration file (20220520152658_AddFormsTable.cs) for the Up / Down logic.
  2. Run database update in development to create the irby.IrbyForms table.
  3. Extend your DbContext with DbSet<IrbyForm> to query the new table.

🚀 Happy Coding!