๐ŸŒŸ Migration Designer: AddColumnsToJobTable.Designer.cs

Path: IrbyImaging.DbContext/Migrations/20240124163357_AddColumnsToJobTable.Designer.cs
Migration: 20240124163357_AddColumnsToJobTable
EF Core Version: 7.0.7 ๐Ÿš€


๐Ÿ“‹ Table of Contents

  1. ๐Ÿ”– Index
  2. ๐Ÿง Overview & Purpose
  3. ๐Ÿ› ๏ธ Key Functional Areas
  4. ๐Ÿ”Œ Dependencies & Technologies
  5. ๐Ÿ“‘ Data Models & Entities
  6. ๐Ÿ” BuildTargetModel Method Explained
  7. ๐Ÿ”„ Migration Workflow & Usage
  8. ๐Ÿ’ก Sample Code Snippets
  9. ๐Ÿ“ˆ UML Diagram
  10. ๐Ÿ“ View Models & APIs
  11. ๐Ÿ› ๏ธ Helper Methods & Utilities
  12. ๐Ÿšซ Error Handling & Return Types
  13. ๐Ÿ”— Thirdโ€‘party Integrations
  14. ๐Ÿ“Œ Conclusion

๐Ÿ”– Index

Property Value
File Name 20240124163357_AddColumnsToJobTable.Designer.cs
Class AddColumnsToJobTable (partial)
DbContext IrbyContext
Build Method BuildTargetModel(ModelBuilder modelBuilder)
Purpose Snapshot EF Core model for the migration

๐Ÿง Overview & Purpose

This auto-generated C# file is part of an Entity Framework Core migration. It:

  • Captures the entire entity model (all tables, columns, relationships) as of this migration.
  • Enables EF Core to diff the model when generating the SQL in the companion Up()/Down() methods.
  • Ensures consistency and reproducibility of the database schema.

๐Ÿšฉ Note: You typically do not modify this file manually. It is re-generated every time you scaffold a migration.


๐Ÿ› ๏ธ Key Functional Areas

  • Global Annotations
    • Product version, max identifier length.
  • Identity Columns Configuration
    • .UseIdentityColumns() for SQL Server autoโ€‘increment.
  • Entity Definitions
    • Properties, data types, nullability, keys, indices.
  • Inheritance Mapping (TPH)
    • Discriminator columns for Asset subclasses.
  • Relationships & Navigation
    • Foreign keys, HasOne/WithMany associations.

๐Ÿ”Œ Dependencies & Technologies

Technology Version Role
.NET SDK 7.0.x Runtime
EntityFrameworkCore.SqlServer 7.0.7 EF Core provider for SQL Server
C# / Roslyn Latest Language & compiler
SQL Server 2016+ Target database

No external NuGet packages or thirdโ€‘party APIs are referenced in this designer file.


๐Ÿ“‘ Data Models & Entities

The designer snapshots 40+ domain classes. Below are a few representative entities:

Entity Class Table & Schema Description
Asset irby.Asset Base for IT/mobile/safety equipment.
AssetHistory irby.AssetHistory Change log for assets.
Job irby.Job Projects/jobs. Focus of this migration.
Employee irby.Employee Company personnel.
FieldOffice irby.FieldOffice Office locations.
Customer, Ticket, TimeSheetEntry, โ€ฆ โ€ฆ Other domain tables.

๐Ÿ“ฆ Focus: Job Entity & New Columns

The Job table receives one or more new/modified columns in this migration.

Column CLR Type SQL Type Notes
JobId Guid uniqueidentifier PK, Identity-style
FieldOfficeId Guid? uniqueidentifier FK โ†’ FieldOffice.Id
JdeNumber string nvarchar(450) New unique business identifier
ManagerId Guid? uniqueidentifier FK โ†’ Employee.EmployeeId
ProjectManagerId Guid? uniqueidentifier FK โ†’ Employee.EmployeeId
SuperintendentId Guid? uniqueidentifier FK โ†’ Employee.EmployeeId
Name string nvarchar(max) Job name
  • ๐Ÿ”‘ Primary Key: JobId
  • ๐Ÿ“Š Indices:
    • Unique on JdeNumber (filter: IS NOT NULL)
    • Nonโ€‘unique on FieldOfficeId

๐Ÿ” BuildTargetModel Method Explained

[DbContext(typeof(IrbyContext))]
[Migration("20240124163357_AddColumnsToJobTable")]
partial class AddColumnsToJobTable
{
    protected override void BuildTargetModel(ModelBuilder modelBuilder)
    {
        // 1๏ธโƒฃ Global annotations
        modelBuilder
            .HasAnnotation("ProductVersion", "7.0.7")
            .HasAnnotation("Relational:MaxIdentifierLength", 128);

        // 2๏ธโƒฃ Identity column support for SQL Server
        SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);

        // 3๏ธโƒฃ Define the Job entity
        modelBuilder.Entity("IrbyImaging.DomainClasses.Job", b =>
        {
            b.Property<Guid>("JobId")
              .ValueGeneratedOnAdd()
              .HasColumnType("uniqueidentifier");

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

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

            // โ€ฆ other properties โ€ฆ

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

        // 4๏ธโƒฃ Relationships & all other entitiesโ€ฆ
    }
}
  1. Annotations: Sets EF Core metadata.
  2. IdentityColumns: Enables SQL Server IDENTITY.
  3. Entity Config:
    • Maps each CLR property โ†’ DB column.
    • Key & index definitions.
  4. Relationships: Configured after all entities (bottom of build).

๐Ÿ”„ Migration Workflow & Usage

  1. Scaffold Migration
    dotnet ef migrations add AddColumnsToJobTable
    
  2. Inspect
    • AddColumnsToJobTable.cs โ†’ Up() / Down() methods.
    • AddColumnsToJobTable.Designer.cs โ†’ model snapshot.
  3. Apply to Database
    dotnet ef database update
    
  4. Verify
    • Check Job table in SQL Server: new JdeNumber column present and unique.

๐Ÿ’ก Sample Code Snippets

Querying a Job by JdeNumber

using var context = new IrbyContext();
var job = await context.Jobs
    .Include(j => j.FieldOffice)
    .FirstOrDefaultAsync(j => j.JdeNumber == "JN-10023");

Console.WriteLine($"Job: {job.Name} (Office: {job.FieldOffice.Name})");

๐Ÿ“ˆ UML Diagram

classDiagram
    class Job {
        +Guid JobId
        +string JdeNumber
        +Guid? FieldOfficeId
        +Guid? ManagerId
        +Guid? ProjectManagerId
        +Guid? SuperintendentId
        +string Name
    }
    class FieldOffice {
        +Guid Id
        +string Name
        // โ€ฆ
    }
    Job --> FieldOffice : FieldOfficeId

Copy into Mermaid Live Editor for visualization.


๐Ÿ“ View Models & APIs

  • ๐Ÿ“‚ View Models: N/A
  • ๐Ÿ”— API Endpoints: N/A

    This file purely concerns EF Core schema snapshot.


๐Ÿ› ๏ธ Helper Methods & Utilities

  • BuildTargetModel: The sole methodโ€”no additional utilities.
  • No manually authored helpers present.

๐Ÿšซ Error Handling & Return Types

  • Error Handling: Not applicable; this is metadata.
  • Return Types: void for BuildTargetModel.
  • Exceptions: EF Core may throw during migrations if the model is invalid.

๐Ÿ”— Thirdโ€‘party Integrations

None in this designer file.
It references only Microsoft EF Core namespaces.


๐Ÿ“Œ Conclusion

The AddColumnsToJobTable.Designer.cs is a critical part of the EF Core migration machinery. It captures a complete snapshot of your domain model at migration time, allowing EF Core to:

  • Compute diffs between models.
  • Generate reliable Up() / Down() scripts.
  • Maintain a versioned history of your database schema.

๐Ÿ”ง Tip: Treat it as readโ€‘onlyโ€‘generated code. Focus your customizations on the Up()/Down() methods or your domain classes.