📚 Documentation: 20240805140229_AddTypeCodeToWorkOrder.Designer.cs


File Path:
IrbyImaging.DbContext/Migrations/20240805140229_AddTypeCodeToWorkOrder.Designer.cs
Purpose: Auto‐generated EF Core migration model snapshot reflecting the addition of a new WorkOrderTypeCode property to the WorkOrder entity.


📑 Table of Contents

  1. ✨ Overview & Purpose
  2. ⚙️ Dependencies & Technologies
  3. 🏗️ Migration Structure
  4. 🔑 Key Functional Areas
  5. 🗂️ Model Snapshot
  6. 📈 Relationships & Navigation
  7. 🛠️ Applying the Migration
  8. ⚠️ Best Practices & Gotchas
  9. ✅ Conclusion

✨ Overview & Purpose

This Designer file is part of Entity Framework Core’s migration scaffolding. When you run:

dotnet ef migrations add AddTypeCodeToWorkOrder

EF generates three files:

  • 20240805140229_AddTypeCodeToWorkOrder.cs
  • 20240805140229_AddTypeCodeToWorkOrder.Designer.cs
  • A snapshot update in your ModelSnapshot

The Designer file contains the BuildTargetModel(ModelBuilder) method—a complete snapshot of your EF Core model after the migration. Its primary role is to allow EF to compare “before” vs. “after” models and generate accurate SQL for Up()/Down().

📌 Do NOT modify this file manually—it’s auto-generated.


⚙️ Dependencies & Technologies

Package/Framework Version Purpose
Microsoft.EntityFrameworkCore 7.0.7 (annotation) Core ORM
Microsoft.EntityFrameworkCore.Infrastructure 7.0.x Infrastructure services
Microsoft.EntityFrameworkCore.Metadata 7.0.x Model metadata
Microsoft.EntityFrameworkCore.Migrations 7.0.x Migrations API
Microsoft.EntityFrameworkCore.Storage.ValueConversion 7.0.x Type conversions
.NET SDK ≥7.0 Platform
SQL Server Any Relational provider (Identity columns enabled)

🏗️ Migration Structure

[DbContext(typeof(IrbyContext))]
[Migration("20240805140229_AddTypeCodeToWorkOrder")]
partial class AddTypeCodeToWorkOrder 
{
    // 1️⃣ BuildTargetModel: entire model after migration
    protected override void BuildTargetModel(ModelBuilder modelBuilder) 
    {
        // Configurations for all entities...
    }
}
  • [DbContext] ties this migration to your IrbyContext.
  • [Migration("…")] stamps the timestamp & name.
  • BuildTargetModel: The big method that re‐creates your EF model, including the newly added column.

🔑 Key Functional Areas

  1. Model Snapshot
    Auto‐generated definitions for every entity, property, column type, keys & indexes.

  2. Identity Configuration
    Usage of UseIdentityColumns() for SQL Server identity behaviors.

  3. Entity Configurations
    Hundreds of .Entity("Namespace.Class", b => { ... }) blocks—one per domain class.

  4. Relationships
    Fluent‐API bridging via HasOne(), WithMany(), HasForeignKey(), OnDelete().

  5. Migration Change
    Addition of WorkOrderTypeCode to the WorkOrder entity.


🗂️ Model Snapshot

Note: The snapshot is vast; below we focus on the WorkOrder entity and its context within the full model.

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

SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);

// … hundreds of entity mappings …

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

    // Existing columns…
    b.Property<DateTime?>("DateAssigned").HasColumnType("datetime2");
    b.Property<DateTime?>("DateComplete").HasColumnType("datetime2");
    b.Property<DateTime?>("DateStart").HasColumnType("datetime2");
    b.Property<string>("DisplayName").HasColumnType("nvarchar(max)");
    // …

    // 🔥 NEW COLUMN added by this migration
    b.Property<string>("WorkOrderTypeCode")
        .HasColumnType("nvarchar(max)");

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

    b.HasKey("WorkOrderId");
    b.HasIndex("JobId");
    b.HasIndex("ProjectManagerId");
    b.ToTable("WorkOrder", "irby");
});

WorkOrder Entity (Change Highlight)

Property CLR Type Column Type Notes
WorkOrderId Guid uniqueidentifier Primary Key, Identity
DateAssigned DateTime? datetime2 Nullable
DateStart DateTime? datetime2 Nullable
DateComplete DateTime? datetime2 Nullable
WorkOrderType string nvarchar(max) E.g. “Repair”, “Inspection”
WorkOrderTypeCode string nvarchar(max) New: Short code (e.g. “INSP”, “RPR”)
WorkOrderNumber string nvarchar(max) Business reference
JobId Guid uniqueidentifier FK → Job(JobId)
ProjectManagerId Guid? uniqueidentifier FK → Employee(EmployeeId) (nullable)
… (plus many more)

📈 Relationships & Navigation

EF Core uses Fluent API to build relationships:

modelBuilder.Entity("IrbyImaging.DomainClasses.WorkOrder", b =>
{
    b.HasOne("IrbyImaging.DomainClasses.Job", "Job")
     .WithMany()
     .HasForeignKey("JobId")
     .OnDelete(DeleteBehavior.Cascade)
     .IsRequired();

    b.HasOne("IrbyImaging.DomainClasses.Employee", "Employee")
     .WithMany()
     .HasForeignKey("ProjectManagerId");
});
  • WorkOrder → Job: Many work orders attach to one job.
  • WorkOrder → Employee: One project manager per work order (optional).

Mermaid UML Diagram

classDiagram
    class WorkOrder {
      +Guid WorkOrderId
      +string DisplayName
      +DateTime? DateAssigned
      +string WorkOrderTypeCode
      +string WorkOrderType
      +Guid JobId
      +Guid? ProjectManagerId
    }

    class Job {
      +Guid JobId
      +string Name
      +string JobNumber
    }

    class Employee {
      +Guid EmployeeId
      +string DisplayName
    }

    WorkOrder --> Job : JobId
    WorkOrder --> Employee : ProjectManagerId

🛠️ Applying the Migration

  1. Add the migration (already done):

    dotnet ef migrations add AddTypeCodeToWorkOrder
    
  2. Update the database:

    dotnet ef database update
    
    • EF will read the Up() method in 20240805140229_AddTypeCodeToWorkOrder.cs and execute:
      ALTER TABLE [irby].[WorkOrder]
        ADD [WorkOrderTypeCode] NVARCHAR(MAX) NULL;
      
  3. Verify

    • In your SQL Server:
      SELECT TOP 10 WorkOrderTypeCode FROM irby.WorkOrder;
      

⚠️ Best Practices & Gotchas

  • 🚫 Do not edit .Designer.cs manually—it will be overwritten next time you scaffold.
  • ✔ Ensure nullable vs non-nullable alignment in your C# model and DB column.
  • 🔄 After adding code references to the new column, re-build and run unit tests.
  • 🧹 If working in source control, review both AddTypeCodeToWorkOrder.cs and .Designer.cs for unintended changes.

✅ Conclusion

This Designer snapshot is a crucial piece in EF Core’s migration pipeline—capturing the model state after you added WorkOrderTypeCode. It ensures accurate SQL generation and schema synchronization across environments. Remember:

  • It’s auto-generated; trust EF to update it for you.
  • Your business logic should reference the new WorkOrderTypeCode property from your domain model (IrbyImaging.DomainClasses.WorkOrder).
  • Keep migrations in source control to maintain a clear upgrade history.

Happy coding! 🚀