📄 Documentation: AddCostCodeType.Designer.cs

📗 Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Key Functional Areas
  4. Data Models & View Models
  5. BuildTargetModel Method
  6. Entity Spotlight: CostCode
  7. Relationships & Conventions
  8. Usage Examples
  9. UML Diagram
  10. Conclusion

📖 Overview & Purpose

File:
IrbyImaging.DbContext/Migrations/20250206152456_AddCostCodeType.Designer.cs

What is it?

  • An auto‑generated EF Core “designer” file (model snapshot)
  • Captures the shape of your EF Core model after the AddCostCodeType migration
  • Used internally by EF Core to compute subsequent migrations

Why it matters

  • Ensures EF Core knows exactly which tables/columns/keys exist
  • Do not edit manually—any changes to your domain classes should be followed by Add-Migration, which regenerates this snapshot

🛠 Dependencies & Technologies

Technology Version
.NET 6.0+
Entity Framework Core 9.0.0
Microsoft.EntityFrameworkCore.SqlServer 9.0.0
C# 10+
SQL Server 2016+

🚀 Key Functional Areas

  • Model Snapshot
    • BuildTargetModel(ModelBuilder modelBuilder) registers every entity
  • Entity Configuration
    • Defines tables, columns, keys, indexes
    • Applies value conversions and identity columns
  • Relationship Wiring
    • Configures FKs, navigation properties & cascade rules
  • Migration Tracking
    • Acts as a baseline for future migrations

🗃️ Data Models & View Models

Note: This file only contains data model (domain) configurations.
There are no view models or API controllers here.

Key Domain Classes Included

  • AccountBalances
  • AccountLedger
  • Asset (+ subclasses: Computer, MobileDevice, …)
  • CostCode
  • Job
  • Employee
  • …and many more (all under schema "irby")

🔧 BuildTargetModel Method

Every EF Core migration designer file implements:

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

    // 2) Entity configurations
    modelBuilder.Entity("Namespace.ClassName", b =>
    {
        b.Property<CLRType>("PropertyName")
         .HasColumnType("sqlType")
         .ValueGeneratedOnAdd();    // e.g. GUID or Identity

        b.HasKey("PrimaryKey");
        b.HasIndex("ForeignKey");
        b.ToTable("TableName", "schema");
    });

    // 3) Relationship wiring
    modelBuilder.Entity("ClassA", b =>
    {
        b.HasOne("ClassB", "NavigationName")
         .WithMany("CollectionName")
         .HasForeignKey("ClassBId")
         .OnDelete(DeleteBehavior.Cascade);
    });
}

Purpose in EF Core Migrations

  • Keeps a snapshot of your model so that EF can compute diffs when you run Add-Migration again
  • Works in tandem with the .cs migration file that contains the Up() and Down() SQL operations

🔎 Entity Spotlight: CostCode

The AddCostCodeType migration’s primary purpose was to add a new integer column CostCodeType to the CostCode table.

Property CLR Type SQL Type Notes
CostCodeId Guid uniqueidentifier PK, ValueGeneratedOnAdd
CostCodeNumber string nvarchar(max)
CostCodeType int int New column
Name string nvarchar(max)
JobId Guid uniqueidentifier FK → Job(JobId)
modelBuilder.Entity("IrbyImaging.DomainClasses.CostCode", b =>
{
    b.Property<Guid>("CostCodeId")
     .ValueGeneratedOnAdd()
     .HasColumnType("uniqueidentifier");

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

    b.Property<int>("CostCodeType")            // <-- New field
     .HasColumnType("int");

    b.Property<Guid>("JobId")
     .HasColumnType("uniqueidentifier");

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

    b.HasKey("CostCodeId");
    b.HasIndex("JobId");
    b.ToTable("CostCode", "irby");
});

🔗 Relationships & Conventions

  • Foreign Keys
    Named {NavigationPropertyName}Id, configured with .HasForeignKey()
  • Cascade Delete
    Applied to required relationships (.IsRequired() + .OnDelete(DeleteBehavior.Cascade))
  • Schema
    Most tables live in the "irby" schema; some use default (dbo)
  • Inheritance
    Asset uses TPH (Table‑Per‑Hierarchy) with a Discriminator column

🧑‍💻 Usage Examples

  1. Apply the Migration

    dotnet ef database update
    
  2. Querying the New Column

    var code = await context.CostCodes.FindAsync(myCostCodeId);
    Console.WriteLine($"Cost Code Type: {code.CostCodeType}");
    
  3. Filter by CostCodeType

    var materialCodes = context.CostCodes
                              .Where(cc => cc.CostCodeType == (int)CostCodeType.Material)
                              .ToList();
    

📊 UML Diagram

+------------------+         1       *        +------------------+
|      Job         |<------------------------|    CostCode      |
+------------------+                         +------------------+
| - JobId : Guid   |                         | - CostCodeId:Guid|
| - JdeNumber: str |                         | - CostCodeNumber |
+------------------+                         | - CostCodeType   |
                                             | - Name           |
                                             | - JobId : Guid   |
                                             +------------------+

📝 Conclusion

  • AddCostCodeType.Designer.cs is a snapshot of your EF Core model after adding CostCodeType.
  • Do not hand-edit—always update your C# domain classes + use Add-Migration.
  • This file underpins EF’s ability to generate the correct SQL when you call database update in the future.

Enjoy clean migrations and a well‑tracked schema! 🎉


Generated by EF Core Tools · Do not modify manually