πŸ“„ Migration Documentation: 20240307125652_ChangeJobIdToNullable.Designer.cs

File Path: IrbyImaging.DbContext/Migrations/20240307125652_ChangeJobIdToNullable.Designer.cs


πŸ“‘ Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Key Functional Areas
  4. BuildTargetModel Method Walkthrough
  5. Entities & Mappings
  6. Relationships & Navigation Properties
  7. Migration Change: JobId β†’ Nullable
  8. How to Use This Migration
  9. Sample Code & CLI Commands
  10. Simplified UML Diagram
  11. Conclusion

πŸ” 1. Overview & Purpose

The Designer file is an auto-generated snapshot of your Entity Framework Core model after applying the migration named ChangeJobIdToNullable. It contains:

  • A BuildTargetModel(ModelBuilder modelBuilder) method.
  • Fluent API calls to configure entities, properties, keys, indexes, discriminators, relationships, and table schemas.
  • The EF Core metadata representing the target state of your database schema.

Why this file exists
EF Core migrations split into two classes:

  1. Migration class (20240307125652_ChangeJobIdToNullable.cs) – holds Up() / Down() to apply/revert changes.
  2. Designer class (…Designer.cs) – holds the BuildTargetModel() snapshot of the entire model.

πŸ› οΈ 2. Dependencies & Technologies

Technology Version
.NET SDK 7.0.x
Entity Framework Core 7.0.7
Database Provider Microsoft SQL Server
Migrations Code-based, Fluent API

Key NuGet Packages:

  • Microsoft.EntityFrameworkCore
  • Microsoft.EntityFrameworkCore.SqlServer
  • Microsoft.EntityFrameworkCore.Design

✨ 3. Key Functional Areas

  1. Model Snapshot
    Captures the state of all EF entities and relations post-migration.

  2. Property Configurations

    • Column types (e.g., nvarchar(max), uniqueidentifier).
    • Identity/auto-generated keys.
    • Nullable vs. required properties.
  3. Table & Schema Mapping

    • Custom schemas (e.g., "irby").
    • Table-per-hierarchy discriminators for polymorphic assets.
  4. Indexes & Constraints

    • Unique filters (e.g., JdeNumber IS NOT NULL).
    • Foreign-key indexes.
  5. Discriminator Configuration

    • Single-table inheritance for Asset subtypes: Computer, MobileDevice, etc.

πŸ”¨ 4. BuildTargetModel Method Walkthrough

protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
    modelBuilder
        .HasAnnotation("ProductVersion", "7.0.7")
        .HasAnnotation("Relational:MaxIdentifierLength", 128);

    // Use SQL Server identity/sequence columns
    SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);

    // Entity-by-entity configuration begins...
    modelBuilder.Entity("IrbyImaging.DomainClasses.Asset", b => { … });
    modelBuilder.Entity("IrbyImaging.DomainClasses.Employee", b => { … });
    // (dozens more)
    
    // Relationship configuration at the end:
    modelBuilder.Entity("IrbyImaging.DomainClasses.Asset", b => {
        b.HasOne("IrbyImaging.DomainClasses.FieldOffice", "FieldOffice")
         .WithMany("Assets")
         .HasForeignKey("FieldOfficeId");
        // ...
    });
}
  • HasAnnotation: Tracks EF Core version & provider specifics.
  • UseIdentityColumns: Configures PKs to auto-increment via IDENTITY in SQL Server.
  • Entity(...) block: Defines properties, keys, indexes, discriminator values, table names, schema.

πŸ“‚ 5. Entities & Mappings

Below is a high‑level summary of the dozens of domain entities defined in this migration. Each entity block maps to a table or view in your database.

Asset Management

Entity Table (Schema) Notes
Asset Asset (irby) TPH root, discriminator column
β€’ Computer same Discriminator: "Computer"
β€’ MobileDevice same Unique IMEI index, custom column mapping
β€’ DeskPhone / Printer same Subtypes of Asset
AssetModel AssetModel (irby) FK β†’ Manufacturer
AssetModelDetail AssetModelDetail Detailed options
Manufacturer AssetManufacturer (irby)
AssetHistory AssetHistory (irby) Tracks audit trail

Document Management

Entity Table (Schema) Notes
DocumentImage DocumentImage (irby) BLOB + OCR + indexing
DocumentAttribute DocumentAttribute (irby) Index field definitions
DocumentCategory DocumentCategory (irby)
DocumentType DocumentType (irby)
BlobData BlobData (irby) Temporary binary storage
TempDocumentData TempDocumentData (irby) Staging area
IrbyForms IrbyForms (irby) Legacy form storage
IPMMigration IPMMigration (irby) Import migration staging

People & HR

Entity Table (Schema) Notes
Employee Employee (irby) FK β†’ Job, Department, Role
Department Department (irby) Organizational unit
Role Role (irby) Security roles
Permissions Permissions (irby)
LinkUserRole LinkUserRole (irby) Junction table
LinkRolePermission LinkRolePermission (irby)
TimeEntryType TimeEntryType (irby) Timesheet categories
TimeSheetEntry TimeSheetEntry (irby) Hours logged

Field Offices & Jobs

Entity Table (Schema) Notes
FieldOffice FieldOffice (irby) FK β†’ multiple Employee roles
Job Job (irby) FK β†’ FieldOffice, unique JDE idx
JobAndFieldOfficeMapping JobAndFieldOfficeMapping Reporting view
WorkOrder WorkOrder (irby) FK β†’ Job

Reporting & Finance

Entity Table / View Notes
BillingInvoice BillingInvoice (irby) Invoicing data
CarrierType / Detail CarrierType (irby), CarrierTypeDetail
GeneralLedger / GLTypeDetail GeneralLedger (irby), GLTypeDetail
PurchaseOrder PurchaseOrder (irby) FK β†’ Job
ContractTypeDetails, ContractTypeNumber, ContractsByLead Views / tables for contract reporting
LeaseDetails, LeasePropertyDetails, DirectorLeaseInfo, DirectorProperties Real estate cost analytics
LicenseReport, GeoLocations, StatePropertyCosts Various reporting tables

Third-Party / ODS Imports

Entity Table Notes
ODSEmployee, ODSCustomer, ODSJob ODSEmployees, ODSCustomers, ODSJobs Imported from external system
OperatingSystem OperatingSystem (irby) Lookups for device OS
Vehicle Vehicle (irby) Light-weight reference
Ticket / Update Ticket (irby), Update (irby) Support ticketing
COVID Test Data CovidTestData (irby) Health screening records
LytxTemplate LytxTemplate (irby) Telematics import snapshot

πŸ”— 6. Relationships & Navigation Properties

  • 1:N

    • FieldOffice β†’ many Assets, Jobs
    • Job β†’ many Employees, WorkOrders
    • Employee β†’ many Assets, many TimeSheetEntry
    • Ticket β†’ many Updates
  • N:1

    • Asset β†’ one Model, one FieldOffice, one Owner (Employee)
  • Many-to-Many (via junction tables)

    • User ↔ Role through LinkUserRole
    • Role ↔ Permission through LinkRolePermission

Discriminator-based inheritance generates a single Asset table for subtypes.


πŸ› οΈ 7. Migration Change: JobId β†’ Nullable

The core change introduced by ChangeJobIdToNullable is to allow the JobId foreign key in certain entities (e.g., Employee, Equipment) to be nullable.

  • Before:
    b.Property<Guid>("JobId")
     .IsRequired()  // non-nullable
     .HasColumnType("uniqueidentifier");
    
  • After:
    b.Property<Guid?>("JobId")
     .HasColumnType("uniqueidentifier");
    
  • Effect: Employees (or other entities) can now exist without an associated Job.

While the Designer snapshot shows the resulting model, the Up() method in the companion file will issue:

migrationBuilder.AlterColumn<Guid?>(
    name: "JobId",
    table: "Employee",
    type: "uniqueidentifier",
    nullable: true,
    oldClrType: typeof(Guid),
    oldType: "uniqueidentifier");

πŸš€ 8. How to Use This Migration

  1. Add Migration
    (Already done)

    dotnet ef migrations add ChangeJobIdToNullable
    
  2. Update Database
    Applies all pending migrations to your SQL Server database:

    dotnet ef database update
    
  3. Inspect Schema
    Verify that Employee.JobId (and any other adjusted columns) now allows NULL.

  4. Roll Back (if needed)

    dotnet ef database update LastGoodMigrationName
    

πŸ’» 9. Sample Code & CLI Commands

# 1. Install EF Tools (if not already)
dotnet tool install --global dotnet-ef

# 2. Scaffold new migration
dotnet ef migrations add ChangeJobIdToNullable -c IrbyContext 

# 3. Apply to DB
dotnet ef database update

Example usage in code:

using (var context = new IrbyContext())
{
    // Now you can create an Employee without a Job:
    var unassigned = new Employee
    {
        FirstName = "Jane",
        LastName  = "Doe",
        JobId     = null,     // Was not possible before!
        Status    = "Active"
    };
    context.Employees.Add(unassigned);
    context.SaveChanges();
}

πŸ“Š 10. Simplified UML Diagram

classDiagram
    FieldOffice "1" --> "*" Asset : Houses
    FieldOffice "1" --> "*" Job   : Manages
    Asset <|-- Computer
    Asset <|-- MobileDevice
    Employee "0..*" --> "1" Job    : WorksOn
    Employee "0..*" --> "*" Asset  : Owns
    Job "1" --> "*" Employee       : Employs
    Job "1" --> "*" WorkOrder      : Contains
    Role "1" --> "*" LinkUserRole  : Grants
    Permission "1" --> "*" LinkRolePermission : Authorizes

Note: This Mermaid diagram is a simplified subset focusing on core classes.


βœ… 11. Conclusion

This Designer snapshot is crucial for EF Core’s migration engine to understand the end state of your model. It:

  • Captures every entity, property, index, and relationship.
  • Ensures your ChangeJobIdToNullable migration can be applied deterministically across environments.
  • Enables you to evolve your domain model safely via code-first migrations.

By reviewing this documentation, you now have:

  • A clear structure of your database schema.
  • Insight into the nullable JobId change.
  • Guidance on running and reverting this migration.

Happy coding! πŸŽ‰