📚 Documentation: 20250310163049_AddCostCodeIdToCrewEquipment.Designer.cs

Location: IrbyImaging.DbContext/Migrations/20250310163049_AddCostCodeIdToCrewEquipment.Designer.cs


📑 Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Key Functional Areas
  4. Data Model & Entities
  5. CrewEquipment Entity & CostCode Addition
  6. Relationships & Fluent API Mapping
  7. Usage & Code Snippets
  8. UML Diagram
  9. Conclusion

🧐 Overview & Purpose

This auto-generated EF Core Migration Designer class represents the model snapshot after applying the AddCostCodeIdToCrewEquipment migration.

  • What it is
    • A C# partial class inheriting from Migration that rebuilds the entire EF Core model in code.
  • Why it exists
    • Tracks schema changes over time.
    • Ensures that subsequent migrations know the current shape of your database.
  • Primary change in this migration
    • Introduction of the CostCodeId foreign‐key property on the CrewEquipment entity/table.

⚙️ Dependencies & Technologies

Technology / Package Version
.NET SDK 7.0+
Microsoft.EntityFrameworkCore 9.0.0
Microsoft.EntityFrameworkCore.SqlServer 9.0.0
C# Language 10+

🔑 Key Functional Areas

1. BuildTargetModel Method

Every EF Core migration designer class contains a single key method:

protected override void BuildTargetModel(ModelBuilder modelBuilder)
  • Purpose
    • Reconstructs the full EF Core model, including all entities, properties, keys, indexes, relationships, and annotations.
  • Flow
    1. Global Annotations (e.g., product version, identity column strategy).
    2. Entity Definitions (via modelBuilder.Entity(...)).
    3. Property Mapping (.Property<>, .HasColumnType).
    4. Keys & Indexes (.HasKey, .HasIndex).
    5. Relationships & Navigation (.HasOne, .WithMany, .OnDelete).

2. Entity Configuration

For each domain class in IrbyImaging.DomainClasses, EF Core generates:

  • A mapping to a table (often with schema irby, e.g. .ToTable("CrewEquipment", "irby")).
  • Column definitions for each property.
  • Primary key and indexes.
  • Relationships via Fluent API.

🗂 Data Model & Entities

This migration snapshot includes 100+ entities. Below is a selected summary of core entities to illustrate structure:

Selected Entities Summary

Entity Class Table Name Schema Key Property Notes
CrewEquipment CrewEquipment irby CrewEquipmentId Newly has CostCodeId FK.
CrewEmployee CrewEmployee irby Composite: CrewId, EmployeeId Manages crew‐staff assignments.
CostCode CostCode irby CostCodeId Represents job cost breakdown.
Crew Crew irby CrewId Groups staff and equipment for field ops.
Equipment Equipment irby EquipmentId Assets tied to jobs.
Job Job irby JobId Core project entity.
Employee Employee irby EmployeeId Company personnel.
FieldTimesheet FieldTimesheet irby TimesheetId Timesheet header for work entries.

Tip: Each modelBuilder.Entity(...) call defines one table+entity mapping.


🆕 CrewEquipment Entity & CostCode Addition

Before Migration

modelBuilder.Entity("CrewEquipment", b => {
    b.Property<Guid>("CrewEquipmentId").ValueGeneratedOnAdd();
    b.Property<Guid>("CrewId");
    b.Property<Guid>("EquipmentId");
    // ... no CostCodeId
});

After Migration

modelBuilder.Entity("CrewEquipment", b => {
    b.Property<Guid>("CrewEquipmentId").ValueGeneratedOnAdd();
    b.Property<Guid>("CrewId");
    b.Property<Guid>("EquipmentId");
    b.Property<Guid>("CostCodeId")        // 🔥 Newly added FK
     .HasColumnType("uniqueidentifier");

    b.HasKey("CrewEquipmentId");
    b.HasIndex("CostCodeId");            // Index on FK
    b.HasIndex("CrewId");
    b.HasIndex("EquipmentId");
    b.HasIndex("JobId");
    b.ToTable("CrewEquipment", "irby");
});
  • CostCodeId
    • Type: uniqueidentifier (maps to Guid).
    • Foreign Key to CostCode table.
  • Indexes
    • Improves join performance when querying by cost code.

🔗 Relationships & Fluent API Mapping

After defining properties and indexes, EF Core wires up relationships:

modelBuilder.Entity("CrewEquipment", b => {
    b.HasOne("IrbyImaging.DomainClasses.CostCode", "CostCode")
     .WithMany()                       // One cost code → many crew‐equipment entries
     .HasForeignKey("CostCodeId")
     .OnDelete(DeleteBehavior.Restrict)
     .IsRequired();

    b.HasOne("IrbyImaging.DomainClasses.Crew", "Crew")
     .WithMany("CrewEquipment")
     .HasForeignKey("CrewId")
     .OnDelete(DeleteBehavior.Restrict)
     .IsRequired();

    b.HasOne("IrbyImaging.DomainClasses.Equipment", "Equipment")
     .WithMany()
     .HasForeignKey("EquipmentId")
     .OnDelete(DeleteBehavior.Restrict)
     .IsRequired();

    b.HasOne("IrbyImaging.DomainClasses.Job", "Job")
     .WithMany()
     .HasForeignKey("JobId")
     .OnDelete(DeleteBehavior.Restrict)
     .IsRequired();
});
  • HasOne…WithMany
    • Configures one‐to‐many relationships.
  • OnDelete(DeleteBehavior.Restrict)
    • Prevents cascade deletes, preserving historical records.

💻 Usage & Code Snippets

1. Adding the Migration

# PowerShell
Add-Migration AddCostCodeIdToCrewEquipment

# .NET CLI
dotnet ef migrations add AddCostCodeIdToCrewEquipment

2. Applying to Database

# PowerShell
Update-Database

# .NET CLI
dotnet ef database update

3. Querying with LINQ

using var db = new IrbyContext();
var assignments = db.CrewEquipment
    .Include(e => e.CostCode)
    .Include(e => e.Equipment)
    .Where(e => e.CrewId == someCrewId)
    .ToList();

🖼 UML Diagram

Below is a simplified UML snippet illustrating CrewEquipment relationships:

@startuml
class CrewEquipment {
  +Guid CrewEquipmentId
  +Guid CrewId
  +Guid EquipmentId
  +Guid CostCodeId
}

class Crew
class Equipment
class CostCode
class Job

Crew "1" --> "0..*" CrewEquipment : has
Equipment "1" --> "0..*" CrewEquipment : uses
CostCode "1" --> "0..*" CrewEquipment : categorizes
Job "1" --> "0..*" CrewEquipment : belongs to
@enduml

You can paste the above into a PlantUML renderer to view the diagram.


🎯 Conclusion

  • This Designer file is an auto‐generated snapshot capturing the complete EF Core model at the point of the AddCostCodeIdToCrewEquipment migration.
  • Its primary purpose is to allow EF Core to calculate subsequent migrations by comparing future model changes against this baseline.
  • The key enhancement is the addition of CostCodeId to the CrewEquipment entity, connecting equipment assignments to cost codes for better financial tracking.

🚀 Now your CrewEquipment entries can be linked directly to CostCodes, enabling richer reporting and integrity in your cost‐accounting workflows.


End of Documentation
Generated with ❤️ for IrbyImaging