πŸ“„ RemoveZipCodeColumn.Designer.cs Documentation

File Path:
IrbyImaging.DbContext/Migrations/20250116130013_RemoveZipCodeColumn.Designer.cs


πŸš€ Table of Contents

  1. Overview & Purpose
  2. Migration Metadata & Annotations
  3. BuildTargetModel: The Model Snapshot
  4. Key Entity Configurations
  5. Relationships & Foreign Keys
  6. Indexes & Constraints
  7. UML Diagrams
  8. How to Use This Designer File
  9. Sample Commands & Usage
  10. Conclusion

1. Overview & Purpose

πŸ” What is this file?
This is an auto-generated EF Core designer file for the RemoveZipCodeColumn migration in the IrbyImaging.DbContext. It contains the BuildTargetModel method, which represents a complete snapshot of the Entity Framework Core model after applying the migration.

πŸ“Œ Why does it exist?

  • Migrations: EF Core tracks schema changes over time. Each migration has two parts:
    • The main migration file (Up/Down methods).
    • A .Designer.cs file capturing the full model state at that point.
  • The designer file ensures EF Core knows how to construct SQL to reach that state and supports scaffolding, diffing, and model snapshotting.

2. Migration Metadata & Annotations

At the top of BuildTargetModel, you’ll see two annotations:

modelBuilder
  .HasAnnotation("ProductVersion", "9.0.0")
  .HasAnnotation("Relational:MaxIdentifierLength", 128);

SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
  • ProductVersion: EF Core version used to generate the model.
  • MaxIdentifierLength: Maximum length for table/column names in the target RDBMS (here, SQL Server = 128).
  • UseIdentityColumns: Configures SQL Server to use IDENTITY for auto-increment keys.

3. BuildTargetModel: The Model Snapshot

The core method:

protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
    // ─── Global annotations ──────────────────────────────────────────────────────
    modelBuilder
        .HasAnnotation("ProductVersion", "9.0.0")
        .HasAnnotation("Relational:MaxIdentifierLength", 128);
    SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);

    // ─── Entity configurations ─────────────────────────────────────────────────
    modelBuilder.Entity("IrbyImaging.DomainClasses.Job", b => { … });
    modelBuilder.Entity("IrbyImaging.DomainClasses.Employee", b => { … });
    modelBuilder.Entity("IrbyImaging.DomainClasses.Asset", b => { … });
    // … dozens of other domain classes …
}

Note: You should never manually edit this file. Changes here will be overwritten by subsequent migrations.


4. Key Entity Configurations

Below are a few representative entities showing how columns, keys, and table/schema names are mapped.

4.1 Job

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

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

    // … other properties …

    b.Property<string>("ZipCode")
     .HasColumnType("nvarchar(max)");

    b.HasKey("JobId");
    b.HasIndex("JdeNumber")
     .IsUnique()
     .HasFilter("[JdeNumber] IS NOT NULL");
    b.ToTable("Job", "irby");
});

4.2 LeaseDetails

This entity captures lease‐specific details extracted from documents.


4.3 Asset Hierarchy

EF Core’s Table‐per‐Hierarchy (TPH) inheritance is used for Asset:

classDiagram
    Asset <|-- Computer
    Asset <|-- DeskPhone
    Asset <|-- MobileDevice
    Asset <|-- OfficePrinter
    Asset : +Guid Id
    Asset : +string Category
    Asset : +string SerialNumber
    Computer : +string AssetNumber
    MobileDevice : +string Imei
    // … more derived types …
  • Discriminator column: identifies subtype
  • All assets are stored in irby.Asset table

4.4 Employee & Department

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

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

    // … address, status, zip …

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

    b.HasOne("IrbyImaging.DomainClasses.Department", "Department")
     .WithMany()
     .HasForeignKey("DepartmentId");

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

    b.ToTable("Employee", "irby");
});
  • Employee βž” references Job (cascade on delete)
  • Employee βž” optional Department

5. Relationships & Foreign Keys

A few common patterns:

Principal Entity Dependent Entity Navigation FK Property Cascade Behavior
FieldOffice Job Jobs FieldOfficeId none
Job Employee Employees JobId Cascade on delete
Employee LinkUserRole User EmployeeId Cascade on delete
Role LinkRolePermission Role RoleId Cascade on delete
GeneralLedger GLTypeDetail GLTypeDetail GeneralLedgerId none

6. Indexes & Constraints

  • Unique Indexes:

    • Job.JdeNumber
    • Employee.JdeNumber
    • FieldOffice.JdeNumber
    • MobileDevice.Imei
  • Composite PKs: None in this snapshot (all use single‐column keys).

  • Default Schemas:

    • Most tables live in irby schema.
    • A few (ODS*) live in default schema without irby.

7. UML Diagrams

7.1 Basic Job–FieldOffice–Employee Relationship

classDiagram
    FieldOffice "1" o-- "*" Job : hosts
    Job "1" *-- "*" Employee : employs

7.2 Asset Inheritance

classDiagram
    Asset <|-- Computer
    Asset <|-- DeskPhone
    Asset <|-- MobileDevice
    Asset : +Guid Id
    Asset : +string Category
    Asset : +string Status
    Computer : +string AssetNumber
    DeskPhone : +string MacAddress
    MobileDevice : +string Imei

8. How to Use This Designer File

  • EF Core Infrastructure will auto‐load this snapshot when you run:
    dotnet ef database update 20250116130013_RemoveZipCodeColumn
    
  • Do NOT modify manuallyβ€”any edits here will be lost.
  • To adjust the schema, modify your domain classes, then run:
    dotnet ef migrations add YourNewChange
    

9. Sample Commands & Usage

# 1. Add a new migration (e.g., removing ZipCode)
dotnet ef migrations add RemoveZipCodeColumn

# 2. Apply migrations to update the database
dotnet ef database update

# 3. Roll back to a previous migration
dotnet ef database update LastGoodMigrationName

πŸ”§ Internally, EF Core will use this .Designer.cs file to compute the diff between the current model snapshot and the one stored in the database.


10. Conclusion

This RemoveZipCodeColumn.Designer.cs file is a critical part of EF Core’s migration machinery:

  • 🎯 It captures a read‐only snapshot of your entire model.
  • 🚧 It’s used to generate SQL during Up/Down operations.
  • πŸ”’ Leave it untouched; treat it as a black box.

By following the patterns above, you’ll maintain a clear, consistent database evolution path as your domain model changes over time. Happy coding! πŸš€βœ¨