🌟 Documentation: ChangeDataTypeOnODSEmployee.Designer.cs

Auto-generated by Entity Framework Core (EF Core) – part of the migration 20240222174838_ChangeDataTypeOnODSEmployee in IrbyImaging.DbContext.Migrations


📋 Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Architecture & Design
  4. BuildTargetModel Method
    4.1. High‑Level Workflow
    4.2. Model Snapshot & Entities
    4.3. ODSEmployee Entity Mapping
    4.4. Fluent‑API Conventions & Relationships
  5. EF Core Migrations: How It Works
  6. Usage & Examples
  7. UML Diagram
  8. Best Practices & Tips
  9. Conclusion

1. Overview & Purpose

File

IrbyImaging.DbContext/Migrations/20240222174838_ChangeDataTypeOnODSEmployee.Designer.cs

This is an auto‑generated snapshot of the EF Core model after applying the migration ChangeDataTypeOnODSEmployee. It is not meant to be edited by hand. Instead, EF Core uses it to:

  • Recreate the complete database schema in memory.
  • Compute diffs when you scaffold future migrations.
  • Ensure consistency between your C# model and the relational schema.

2. Dependencies & Technologies

Component Version / Description
.NET 7.0.7
EntityFrameworkCore Microsoft.EntityFrameworkCore 7.0.7
EF Core Relational (SQL Server) Microsoft.EntityFrameworkCore.SqlServer
EF Core Tools Microsoft.EntityFrameworkCore.Tools
Language C# 11
Database Provider Microsoft SQL Server (Identity Columns used)

3. Architecture & Design

  • Partial Class:
    [DbContext(typeof(IrbyContext))]
    [Migration("20240222174838_ChangeDataTypeOnODSEmployee")]
    partial class ChangeDataTypeOnODSEmployee
    
  • Method:
    protected override void BuildTargetModel(ModelBuilder modelBuilder) { … }
    
  • Role: Represents the target model (post‑migration) so EF can scaffold diffs.

4. BuildTargetModel Method

4.1. High‑Level Workflow

  1. Annotations
    • Sets product version and identifier-length limits.
  2. Use Identity Columns
    • Configures SQL Server’s identity/auto‑increment pattern.
  3. Entity Definitions
    • For each CLR class (domain entity), calls modelBuilder.Entity(...).
  4. Fluent‑API Configuration
    • Properties (.Property<>()), keys (.HasKey()), indexes, table mapping (.ToTable()), discriminators.
  5. Relationships
    • Configured via .HasOne(...).WithMany(...).HasForeignKey(...).

4.2. Model Snapshot & Entities

  • > 70 entities under the namespace IrbyImaging.DomainClasses
  • Examples include:
    • Asset / Computer / MobileDevice (TPH inheritance)
    • Employee, Department, Role, Permission
    • FieldOffice, Job, Customer
    • ODSCustomer, ODSEmployee, ODSJob (ODS = Operational Data Store)
    • Ticket, Update, TimeSheetEntry, TimeEntryType
    • DocumentImage, DocumentType, DocumentAttribute, etc.

All entities are mapped with column types, required rules, and relational constraints.


4.3. ODSEmployee Entity Mapping

This migration focuses on changing data types for ODSEmployee. The snapshot below shows the post‑migration mapping:

modelBuilder.Entity("IrbyImaging.DomainClasses.ODSEmployee", b =>
{
    b.Property<string>("ADDRESS1")
     .HasColumnType("nvarchar(max)");
    b.Property<string>("CITY")
     .HasColumnType("nvarchar(max)");
    // … many other string/date/decimal properties …
    
    // ← Changed property type: EMPLOYEE_ID
    b.Property<byte[]>("EMPLOYEE_ID")
     .HasColumnType("varbinary(max)");

    // EMPLOYEE_NUMBER remains decimal
    b.Property<decimal>("EMPLOYEE_NUMBER")
     .HasColumnType("decimal(18,0)");

    // More metadata …
    b.HasKey(/* no explicit key: table may be read-only or keyless in snapshot */);
    b.ToTable("ODSEmployees");
});

🔄 Summary of Key Columns

Column .NET Type SQL Type Notes
ADDRESS1, CITY, … string nvarchar(max) Default max-length Unicode text
EMPLOYEE_ID byte[] varbinary(max) Changed: storing binary GUID
EMPLOYEE_NUMBER decimal decimal(18,0) Numeric with 0 decimal places
BIRTH_DATE, … DateTime? datetime2 Nullable date/time
… … … …

Note: The actual diff (Up/Down) resides in 20240222174838_ChangeDataTypeOnODSEmployee.cs.


4.4. Fluent‑API Conventions & Relationships

  • Inheritance (TPH) is used for Asset and its subtypes (Computer, Monitor, MobileDevice, etc.):
    modelBuilder.Entity("IrbyImaging.DomainClasses.Computer", b =>
    {
      b.HasBaseType("IrbyImaging.DomainClasses.Asset");
      b.HasDiscriminator().HasValue("Computer");
      // … 
    });
    
  • Indexes:
    b.HasIndex("JdeNumber")
     .IsUnique()
     .HasFilter("[JdeNumber] IS NOT NULL");
    
  • Foreign Keys & Navigations:
    modelBuilder.Entity("IrbyImaging.DomainClasses.Asset", b =>
    {
      b.HasOne("IrbyImaging.DomainClasses.FieldOffice", "FieldOffice")
       .WithMany("Assets")
       .HasForeignKey("FieldOfficeId");
      // …
    });
    

5. EF Core Migrations: How It Works 🚀

  1. Changes in your DbContext or Entities
  2. Add-Migration (PMC) or dotnet ef migrations add
  3. EF Core generates TWO files:
    • Migration (Up() / Down() logic)
    • Designer (this file—model snapshot)
  4. Update-Database applies changes to the SQL database.

6. Usage & Examples 🛠

6.1. Add a Migration

dotnet ef migrations add ChangeDataTypeOnODSEmployee

6.2. Apply to Database

dotnet ef database update

6.3. Inspect Snapshot

  • Open …/Migrations/20240222174838_ChangeDataTypeOnODSEmployee.Designer.cs
  • Look at BuildTargetModel for full schema.

7. UML Diagram 📈

@startuml
package Migrations {
  class ChangeDataTypeOnODSEmployee {
    + BuildTargetModel(modelBuilder)
  }
}
package DomainClasses {
  class ODSEmployee {
    ADDRESS1: string
    CITY: string
    EMPLOYEE_ID: byte[]
    EMPLOYEE_NUMBER: decimal
    // …
  }
}
Migrations.ChangeDataTypeOnODSEmployee ..> DomainClasses.ODSEmployee : snapshot
@enduml

8. Best Practices & Tips 👍

  • Do not edit designer files manually—they will be overwritten.
  • Keep your domain classes in sync with your migrations.
  • Use descriptive migration names for clarity.
  • Test migrations on a local/dev database before CI/CD deployment.
  • Regularly clean up old/unused migrations to reduce complexity.

9. Conclusion

The ChangeDataTypeOnODSEmployee.Designer.cs file is a cornerstone of EF Core’s migration infrastructure. It captures the entire model state after applying the data‑type changes on ODSEmployee and dozens of other entities. By understanding its structure—especially the BuildTargetModel method—you gain insight into how EF Core translates your C# classes and fluent configuration into a concrete SQL schema.

🚀 Next Steps:

  • Review the corresponding Up()/Down() logic in the main migration file.
  • Run dotnet ef migrations script to generate a SQL diff.
  • Incorporate migration testing into your CI/CD pipeline.

Happy coding! 🖥️✨