📋 MoreUpdatesToJobTable3.Designer.cs

Generated on 2024‑01‑30 for IrbyImaging.DbContext Migrations


📑 Table of Contents

  1. Overview & Purpose
  2. EF Core Migrations & .Designer.cs Files
  3. BuildTargetModel Method
  4. Key Functional Areas
    4.1 Domain Entities & Tables
    4.2 Asset Inheritance (TPH)
    4.3 Job Table Structure & Indices
    4.4 Core Relationships & UML Diagram
  5. Dependencies & Technologies
  6. How to Use This Snapshot
  7. Sample Code Snippets
  8. Conclusion & Best Practices

🧐 Overview & Purpose

This auto‑generated file is a model snapshot created by Entity Framework Core (EF Core) during the migration 20240130143555_MoreUpdatesToJobTable3. Its sole responsibility is to reproduce the entire database schema from your IrbyContext in code, so that EF Core can:

  • Compare against your C# POCOs and detect schema drift
  • Generate incremental migrations
  • Reconstruct the model at design‑time

Note: Do not manually edit this file. Any changes here will be overwritten when you add or remove migrations.


🔧 EF Core Migrations & .Designer.cs Files

When you run dotnet ef migrations add, EF Core creates two files:

  1. <timestamp>_MoreUpdatesToJobTable3.cs – Contains the Up() / Down() logic (DDL operations).
  2. <timestamp>_MoreUpdatesToJobTable3.Designer.cs – Contains the snapshot of your ModelBuilder configuration at that point in time.

This snapshot is used to:

  • Track what the model looked like when the migration was created.
  • Prevent accidental data loss by understanding which tables/columns would be dropped.
  • Support design‑time scaffolding and EF Core tools.

🔨 BuildTargetModel Method

At the heart of this file lies:

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

    // Use SQL Server identity columns by default
    SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);

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

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

        b.HasKey("JobId");

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

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

    // ... and hundreds of other entities
}

Key responsibilities of BuildTargetModel:

  • Define tables & schemas (ToTable("X", "irby")).
  • Declare columns (HasColumnType, ValueGeneratedOnAdd).
  • Set up keys & indices (HasKey, HasIndex).
  • Wire relationships (HasOne(...).WithMany(...).HasForeignKey(...)).
  • Configure inheritance via TPH (Table‑Per‑Hierarchy) and Discriminator columns.

🚀 Key Functional Areas

4.1 Domain Entities & Tables

Over 100 domain classes are mapped. Here is a sample summary of core tables:

Entity Table Name Primary Key Notable FKs / Indices
Asset irby.Asset Id (GUID) FieldOfficeId, ModelId, OwnerId
Computer (Derived) irby.Asset Id OperatingSystemId
AssetModel irby.AssetModel Id (int) MakeId
FieldOffice irby.FieldOffice Id (GUID) Various employee FKs
Job irby.Job JobId (GUID) FieldOfficeId, Unique JDE index
Employee irby.Employee EmployeeId DepartmentId, JobId, RoleId
Ticket irby.Ticket Id (GUID) –
… and many more 📦

Tip: You can search modelBuilder.Entity("IrbyImaging.DomainClasses.<YourClass>") to find any specific mapping.


4.2 Asset Inheritance (TPH)

All asset types (Computer, Dock, MobileDevice, Monitor, OfficePrinter, Printer, SafetyEquipment, DeskPhone, NetworkEquipment) share the same table irby.Asset using TPH:

b.HasDiscriminator<string>("Discriminator")
 .HasValue("Asset")            // base
 .HasValue("Computer")         // derived
 .HasValue("MobileDevice")     // derived
 // ...
 .UseTphMappingStrategy();
Discriminator Value CLR Type
Asset Asset
Computer Computer
MobileDevice MobileDevice
… …

4.3 Job Table Structure & Indices

The Job entity received further updates in this migration:

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

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

    b.HasKey("JobId");

    // New: Unique index on JDE number
    b.HasIndex("JdeNumber")
     .IsUnique()
     .HasFilter("[JdeNumber] IS NOT NULL");

    // Foreign key to FieldOffice
    b.HasIndex("FieldOfficeId");

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

Highlights

  • JobId: Primary Key, GUID, auto‑generated.
  • JdeNumber: Business identifier, now indexed uniquely.
  • FK → FieldOffice: A job is tied to a specific field office.

4.4 Core Relationships & UML Diagram

Below is a simplified UML class diagram using Mermaind to illustrate core relationships:

classDiagram
    class Job {
      +GUID JobId
      +string JdeNumber
      +int    Status
      // ...
    }
    class FieldOffice {
      +GUID Id
      +string Name
      // ...
    }
    class Employee {
      +GUID EmployeeId
      +string FirstName
      +string LastName
      // ...
    }
    class Asset {
      +GUID Id
      +string Status
      // ...
    }

    Job "1" -- "0..*" Employee   : employs >
    Job "0..*" -- "1" FieldOffice: belongs to >
    FieldOffice "1" -- "0..*" Asset : houses >
    Employee "1" -- "0..*" Asset    : owns >

🔗 Dependencies & Technologies

  • .NET 7.0
  • Entity Framework Core 7.0.7
  • Microsoft.EntityFrameworkCore.SqlServer (for SQL Server-specific mappings)
  • C# 11 with nullable reference types disabled (#nullable disable)

No third‑party libraries are referenced in this snapshot.


🛠 How to Use This Snapshot

  1. When adding a new migration (dotnet ef migrations add MyMigration), EF Core will compare your current ModelBuilder configuration (in your DbContext.OnModelCreating) with this snapshot.
  2. If differences exist, it scaffolds a new migration that will transform the database from this snapshot to the new model.
  3. At runtime, EF Core uses this snapshot to build the model for IrbyContext.

Never hand‑edit this file. Instead, adjust your domain classes or your OnModelCreating override.


📌 Sample Code Snippets

1. Configuring a New Entity

If you add a new PurchaseOrder class, EF Core would generate:

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

    b.Property<DateTime>("OrderDate")
     .HasColumnType("datetime2");

    b.HasKey("PurchaseOrderId");
    b.ToTable("PurchaseOrder", "irby");
});

2. Adding a Foreign Key

b.HasOne("IrbyImaging.DomainClasses.Job", "Job")
 .WithMany("Employees")
 .HasForeignKey("JobId")
 .OnDelete(DeleteBehavior.Cascade)
 .IsRequired();

This snippet shows how EF Core wires Employee.JobId to Job.JobId with cascade delete.


🏁 Conclusion & Best Practices

  • Purpose: This .Designer.cs file is a read‑only snapshot of your EF Core model at a specific migration point.
  • Do Not Modify: All changes should happen in your entity classes or OnModelCreating.
  • Keep Migrations Clean: Only add migrations for discrete, logical changes—such as “Add JdeNumber index to Job”.
  • Review Snapshots: While you never hand‑edit snapshots, understanding them helps when merging or diagnosing migration conflicts.

📝 Happy coding and may your schema always stay in sync!