πŸ“š IrbyImaging.DbContext Migrations

20250212153204_ChangeTypeOnClassNumber.Designer.cs


πŸš€ Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Key Functional Areas
  4. Entity Snapshots & Data Models
    4.1 JobClassification – ClassNumber Change
    4.2 Asset Hierarchy & Discriminators
    4.3 Relationships & Navigation Properties
  5. Helper Methods & Utilities
  6. Usage & Examples
  7. UML Diagram – Selected Model
  8. Conclusion

πŸ” Overview & Purpose

This auto-generated EF Core migration designer file captures the target model snapshot for the migration named:

ChangeTypeOnClassNumber
Timestamp: 2025-02-12 15:32:04

When you add or update an EF Core migration, the .Designer.cs file is generated to record the entire state of your DbContext model after applying that migration.

  • It drives up/down diff calculations
  • Ensures subsequent migrations know the β€œbefore” and β€œafter” shapes
  • It is not meant to be edited manually

❗ Important:

Always use dotnet ef migrations add or Update-Database rather than handcrafting this.


πŸ› οΈ Dependencies & Technologies

Package Version Purpose
Microsoft.EntityFrameworkCore β˜… (9.0.0) ORM base
Microsoft.EntityFrameworkCore.Relational 9.0.0 Relational database support
Microsoft.EntityFrameworkCore.SqlServer 9.0.0 SQLΒ Server-specific functionality
Microsoft.EntityFrameworkCore.Infrastructure 9.0.0 Internal EF infrastructure
Microsoft.EntityFrameworkCore.Metadata 9.0.0 Model metadata
Microsoft.EntityFrameworkCore.Storage.ValueConversion 9.0.0 Conversion of CLR types ↔ store types
  • Target: .NETΒ 7+ / .NETΒ 8
  • Database: SQLΒ Server (identity columns used)
  • Annotations:
    • ProductVersion = "9.0.0"
    • Relational:MaxIdentifierLength = 128

🧩 Key Functional Areas

  1. Global Model Configuration

    • modelBuilder.HasAnnotation(...)
    • SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder)
  2. Entity Type Definitions

    • modelBuilder.Entity("Namespace.DomainClass", b => { ... })
    • Property mappings: .Property<T>("Name").HasColumnType("...")
    • Keys: .HasKey("PrimaryKeyProperty")
    • Tables & Schemas: .ToTable("TableName", "SchemaName")
    • Indexes: .HasIndex("Property").IsUnique()
  3. Inheritance / Discriminators

    • Table‑Per‑Hierarchy (TPH) patterns
    • .HasDiscriminator().HasValue("TypeName")
  4. Relationships & Navigation Properties

    • .HasOne(...).WithMany(...).HasForeignKey("...")
    • OnDelete behaviors, cascading rules

πŸ—„οΈ Entity Snapshots & Data Models

This migration snapshot includes over 100 domain classes. Below we highlight critical or changed parts.

4.1 JobClassification – ClassNumber Change

πŸ”§ Migration Goal: Change the CLR type of ClassNumber
from int β†’ string

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

    // πŸ”„ Changed here:
    b.Property<string>("ClassNumber")
        .HasColumnType("nvarchar(max)");

    b.Property<string>("Name").HasColumnType("nvarchar(max)");
    b.Property<Guid>("JobId").HasColumnType("uniqueidentifier");
    // … other properties …

    b.HasKey("JobClassificationId");
    b.HasIndex("JobId");
    b.ToTable("JobClassification", "irby");
});
  • Before: ClassNumber mapped to int (int, non-nullable)
  • After: ClassNumber mapped to string (nvarchar(max), non-nullable)

This enables alphanumeric codes, leading zeros, or flexible formatting.


4.2 Asset Hierarchy & Discriminators

The Asset entity uses TPH with a Discriminator column to store subtypes:

CLR Type Discriminator Notes
Asset "Asset" Base class
Computer "Computer" Adds CpsNumber, OperatingSystemId
MobileDevice "MobileDevice" Imei, PhoneNumber, Carrier, Type
... ... ...

Example: MobileDevice mapping

modelBuilder.Entity("IrbyImaging.DomainClasses.MobileDevice", b => {
    b.HasBaseType("IrbyImaging.DomainClasses.Asset");

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

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

    b.ToTable("Asset", "irby");
    b.HasDiscriminator().HasValue("MobileDevice");
});

4.3 Relationships & Navigation Properties

Most entities are wired via foreign keys:

modelBuilder.Entity("IrbyImaging.DomainClasses.Asset", b => {
    b.HasOne("IrbyImaging.DomainClasses.FieldOffice", "FieldOffice")
      .WithMany("Assets")
      .HasForeignKey("FieldOfficeId");

    b.HasOne("IrbyImaging.DomainClasses.AssetModel", "Model")
      .WithMany()
      .HasForeignKey("ModelId")
      .OnDelete(DeleteBehavior.Cascade)
      .IsRequired();

    b.Navigation("FieldOffice");
    b.Navigation("Model");
});
  • One‑to‑Many: e.g., FieldOffice β†’ many Asset
  • Cascade Delete: configured for certain critical dependencies
  • Indices: created on FK columns to speed up joins

πŸ› οΈ Helper Methods & Utilities

In this .Designer.cs you’ll notice:

  • SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder)
    β‡’ Configures all integer keys to use SQLΒ Server IDENTITY by default.

  • #pragma warning disable 612, 618
    β‡’ Suppresses deprecation warnings if your domain classes still use older APIs.

There are no custom extension methods hereβ€”it’s pure EF Core scaffolding.


πŸ’‘ Usage & Examples

  1. Applying this Migration

    dotnet ef database update 20250212153204_ChangeTypeOnClassNumber
    

    This will ALTER the JobClassification table, converting its ClassNumber column to nvarchar(max).

  2. Reverting

    dotnet ef database update <Previous_Migration_Name>
    
  3. Inspecting the SQL

    dotnet ef migrations script 20250212153204_ChangeTypeOnClassNumber
    

    β†’ See the exact ALTER TABLE commands.


πŸ“ˆ UML Diagram – Selected Model

Below is a simplified UML class diagram illustrating the Job ⇄ JobClassification relationship:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         1       *      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚     Job           │──────────────────────▢ β”‚  JobClassification     β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€                         β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ β€’ JobId : Guid    β”‚                         β”‚ β€’ JobClassificationId  β”‚
β”‚ β€’ Name : string   β”‚                         β”‚ β€’ ClassNumber : string β”‚
β”‚ β€’ JdeNumber : str β”‚                         β”‚ β€’ CostCodeNumber : int β”‚
β”‚                    β”‚                         β”‚ β€’ Name : string        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
  • A Job can have multiple JobClassification entries.
  • Cascade delete ensures if a Job is removed, its classifications go, too.

🏁 Conclusion

This ChangeTypeOnClassNumber.Designer.cs is your snapshot of the entire EF Core model after altering the ClassNumber property on JobClassification from an integer to a string.

  • It guarantees consistency across migrations
  • Auto‑generated: avoid manual edits!
  • Key takeaway: Your domain is now flexible to alphanumeric class codes πŸŽ‰

Happy coding! πŸš€