πŸ“„ Migration Designer Documentation

File: IrbyImaging.DbContext/Migrations/20240130145814_MoreUpdatesToJobTable4.Designer.cs


🌟 Table of Contents

  1. πŸ” Overview & Purpose
  2. πŸ› οΈ Dependencies & Technologies
  3. 🌐 File Structure & Naming Conventions
  4. πŸ—οΈ The BuildTargetModel Method
  5. πŸ“‹ Key Entity: Job
  6. πŸ“Š Domain Model at a Glance
  7. πŸŒ€ How EF Core Uses This Designer File
  8. πŸš€ Applying & Reverting the Migration
  9. πŸ“ Sample Code Snippets
  10. πŸ“ˆ UML Class Diagram (Excerpt)
  11. βœ… Conclusion

πŸ” Overview & Purpose

When you run dotnet ef migrations add MoreUpdatesToJobTable4, Entity Framework Core scaffolds two files:

  • A migration file containing Up()/Down() methods (to apply or rollback changes).
  • A designer file (this one), which contains a snapshot of the entire EF Core model after the migration.

Why is this important?
The designer file ensures EF Core knows the complete model state. On subsequent migrations, EF Core diffs the current model against this snapshot to generate the next migration.


πŸ› οΈ Dependencies & Technologies

Dependency Version / Notes
Microsoft.EntityFrameworkCore 7.0.7
Microsoft.EntityFrameworkCore.SqlServer Uses UseIdentityColumns()
Microsoft.EntityFrameworkCore.Metadata ModelBuilder API
C# / .NET 7 Target framework for code generation

🌐 File Structure & Naming Conventions

IrbyImaging.DbContext
└── Migrations
    β”œβ”€β”€ 20240130145814_MoreUpdatesToJobTable4.cs          # Migration operations
    └── 20240130145814_MoreUpdatesToJobTable4.Designer.cs # Model snapshot
  • Timestamp prefix (20240130145814_) keeps migrations ordered.
  • MoreUpdatesToJobTable4: descriptive name of the change set.
  • .Designer.cs: auto-generated, do not edit manually.

πŸ—οΈ The BuildTargetModel Method

At the heart of this file is:

protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
    modelBuilder
      .HasAnnotation("ProductVersion", "7.0.7")
      .HasAnnotation("Relational:MaxIdentifierLength", 128);

    SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);

    // ... Hundreds of modelBuilder.Entity<>() calls ...
}
  • Purpose: Describes every entity, property, key, index, table name, discriminator, and relationship in your DbContext after this migration.
  • Why? EF Core uses it to compute the delta when you add the next migration.

πŸ“‹ Key Entity: Job

This migration’s name suggests changes around the Job table. Inspecting the snapshot:

modelBuilder.Entity("IrbyImaging.DomainClasses.Job", b =>
{
    b.Property<Guid>("JobId")
     .ValueGeneratedOnAdd()
     .HasColumnType("uniqueidentifier");

    b.Property<string>("JdeNumber")
     .HasColumnType("nvarchar(450)");

    // ... other properties ...

    b.HasKey("JobId");

    // πŸ‘€ Here's the newly added unique index on JdeNumber:
    b.HasIndex("JdeNumber")
     .IsUnique()
     .HasFilter("[JdeNumber] IS NOT NULL");

    b.ToTable("Job", "irby");
});

What changed?

  1. JdeNumber configured as nvarchar(450) (instead of nvarchar(max)), making it indexable.
  2. A unique index ensures no two Jobs share the same JDE number.

πŸ“Š Domain Model at a Glance

This snapshot lists all entities. We’ll group them by subdomain:

Subdomain Entities
Asset Management Asset (TPH), AssetHistory, AssetModel, AssetModelDetail, Manufacturer, OperatingSystem, Computer, MobileDevice, …
Document Imaging DocumentImage, DocumentType, DocumentCategory, DocumentAttribute, BlobData, TempDocumentData, IPMMigration, IrbyForms
People & Roles Employee, Department, Role, Permissions, LinkUserRole, LinkRolePermission
Jobs & Offices Job, FieldOffice, JobAndFieldOfficeMapping, ContractsByLead, DirectorProperties, …
Billing & Finance BillingInvoice, GeneralLedger, GLTypeDetail, CarrierType, CarrierTypeDetail
Audit & Reports AuditDetail, AuditReport, LicenseReport, ODSCustomer, ODSEmployee, ODSJob
Time & Tickets TimeEntryType, TimeSheetEntry, Ticket, Update, Issue

Note: Some tables (e.g., ContractTypeDetails, DirectorLeaseInfo) serve reporting or read‐only views.


πŸŒ€ How EF Core Uses This Designer File

  1. Snapshot: Records the last known model state for migrations.
  2. Diffing: When you run Add-Migration, EF Core loads:
    • Your current DbContext model (via data annotations & Fluent API).
    • The snapshot in the .Designer.cs.
    • Computes changes β†’ generates Up()/Down() code.
  3. Model Validation: At runtime, EF Core ensures the target DB matches the snapshot before applying Up().

πŸš€ Applying & Reverting the Migration

# Apply migrations up to this point
dotnet ef database update  

# To rollback this specific migration
dotnet ef database update 20240130145813_PreviousMigration  

Tip: Always commit both .cs and .Designer.cs to source control together.


πŸ“ Sample Code Snippets

1. Unique Index on JdeNumber

b.Property<string>("JdeNumber")
 .HasColumnType("nvarchar(450)");

b.HasIndex("JdeNumber")
 .IsUnique()
 .HasFilter("[JdeNumber] IS NOT NULL");

2. Table‐Per‐Hierarchy (TPH) for Asset

modelBuilder.Entity("IrbyImaging.DomainClasses.Asset", b =>
{
    b.HasDiscriminator<string>("Discriminator")
     .HasValue("Asset");

    // Derived types share the same table
    b.UseTphMappingStrategy();
});

3. Relationships Example (Job β†’ FieldOffice)

modelBuilder.Entity("IrbyImaging.DomainClasses.Job")
  .HasOne("IrbyImaging.DomainClasses.FieldOffice", null)
  .WithMany("Jobs")
  .HasForeignKey("FieldOfficeId");

πŸ“ˆ UML Class Diagram (Excerpt)

@startuml
class Job {
  + Guid JobId
  + string Name
  + string JdeNumber
  + int Status
}

class FieldOffice {
  + Guid Id
  + string Name
  + string JdeNumber
}

Job "N" --> "1" FieldOffice : FieldOfficeId
@enduml

Above: Job holds a foreign key to FieldOffice.


βœ… Conclusion

This designer file is a complete snapshot of your EF Core model after the MoreUpdatesToJobTable4 migration.

  • Never hand-edit it.
  • It’s crucial for EF Core’s diffing algorithm.
  • Shows how every entity, property, key, index, and relationship is configured.

By understanding the BuildTargetModel method and its contents, you gain insight into your database schema as EF Core sees itβ€”empowering you to troubleshoot migrations, optimize queries, and maintain a clean data layer.

Happy coding! πŸš€