📑 IrbyImaging.DbContext Migration

20220509142849_UpdateEmployeeTable.Designer.cs


File Path: IrbyImaging.DbContext/Migrations/20220509142849_UpdateEmployeeTable.Designer.cs
Purpose: EF Core–generated snapshot of the target model after applying the UpdateEmployeeTable migration.
Author: kvamshi
Generated On: 05‑17‑2022


📖 Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Migration Class & Metadata
  4. BuildTargetModel Method
  5. Sample Code Snippet
  6. UML Diagram (Mermaid)
  7. EF Core Migrations Workflow
  8. Usage Examples
  9. Best Practices & Maintenance
  10. Conclusion

🛠 1. Overview & Purpose

This .Designer.cs file is automatically generated by Entity Framework Core whenever you add or update a migration. It:

  • Captures the entire snapshot of your EF Core model (all entities, properties, relationships, indices) at the time of the UpdateEmployeeTable migration.
  • Enables EF Core to compute diffs between the current model and previous snapshots when you run dotnet ef migrations add ….
  • Should not be manually edited—changes go into your model classes or the main migration file.

📦 2. Dependencies & Technologies

Technology / Package Version
.NET 5.0.x
Microsoft.EntityFrameworkCore 5.0.12
Microsoft.EntityFrameworkCore.SqlServer 5.0.12
Microsoft.EntityFrameworkCore.Relational 5.0.12
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

🔍 3. Migration Class & Metadata

[DbContext(typeof(IrbyContext))]
[Migration("20220509142849_UpdateEmployeeTable")]
partial class UpdateEmployeeTable : Migration
  • [DbContext(typeof(IrbyContext))]
    Links this migration to your custom IrbyContext class.

  • [Migration("20220509142849_UpdateEmployeeTable")]
    Unique identifier (timestamp + name) used by EF Core to order migrations.


🔧 4. BuildTargetModel Method

Every EF Core migration designer file contains one main method:

protected override void BuildTargetModel(ModelBuilder modelBuilder)
{ 
    // … auto‑generated snapshot code …
}

🏁 4.1 Global Model Annotations

modelBuilder
  .HasAnnotation("Relational:MaxIdentifierLength", 128)
  .HasAnnotation("ProductVersion", "5.0.12")
  .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
  • MaxIdentifierLength: SQL Server limit (128).
  • ProductVersion: EF Core version.
  • ValueGenerationStrategy: Default identity column behavior.

🏗 4.2 Entity Definitions

The file enumerates 30+ entities under the IrbyImaging.DomainClasses namespace.
Below is a sample summary of a few key entities.

Entity Table (Schema) Purpose
Employee irby.Employee Staff member details
Job irby.Job Project or assignment
FieldOffice irby.FieldOffice Physical office location
Asset (base) irby.Asset Company‐owned asset (TPH root)
Computer, DeskPhone, … irby.Asset Asset sub‐types via TPH
AssetModel irby.AssetModel Models of hardware
BillingInvoice irby.BillingInvoice Invoicing records
Ticket, Update irby.Ticket / irby.Update Support tickets & updates
CovidTestData irby.CovidTestData Health screenings
… and many more.

🔗 4.3 Relationships & Navigation

  • Foreign Keys are configured via .HasForeignKey(...).
  • Navigations use .WithMany(...) or .WithOne(...).

Example:
Employee → Job (each Employee has one CurrentJob)

modelBuilder.Entity("IrbyImaging.DomainClasses.Employee", b => {
  b.HasOne("IrbyImaging.DomainClasses.Job", "CurrentJob")
   .WithMany("Employees")
   .HasForeignKey("CurrentJobId")
   .OnDelete(DeleteBehavior.Cascade)
   .IsRequired();
});

🧬 4.4 Inheritance & Discriminators (TPH)

All Asset subtypes share the same irby.Asset table.

  • Root: IrbyImaging.DomainClasses.Asset
  • Subclasses: Computer, DeskPhone, Dock, MobileDevice, Monitor, NetworkEquipment, OfficePrinter, Printer, SafetyEquipment

Discriminator column:

b.HasDiscriminator<string>("Discriminator")
 .HasValue("Asset")         // root
 .HasValue("Computer")
 .HasValue("DeskPhone")
 // …

🎯 4.5 Indices & Constraints

  • Standard:
    .HasIndex("FieldOfficeId");
  • Unique:
    .HasIndex("JdeNumber").IsUnique().HasFilter("[JdeNumber] IS NOT NULL");

TIP: Unique filtered indices help avoid multiple NULLs interfering with uniqueness.


📝 5. Sample Code Snippet

Below is how EF Core wires up the Employee entity in code:

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

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

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

  b.HasKey("Id");

  b.HasIndex("JdeNumber")
   .IsUnique()
   .HasFilter("[JdeNumber] IS NOT NULL");

  b.ToTable("Employee", "irby");
});

📊 6. UML Diagram (Mermaid)

classDiagram
  Asset <|-- Computer
  Asset <|-- DeskPhone
  Asset <|-- MobileDevice
  Asset <|-- Monitor
  Asset <|-- Printer
  Asset <|-- SafetyEquipment

  Employee "1" o-- "0..*" Asset : owns >
  Employee "1" o-- "1" Job : currentJob >
  FieldOffice "1" o-- "0..*" Asset : locatedAt >
  FieldOffice "1" o-- "1" Employee : manager >
  Ticket "1" o-- "0..*" Update : updates >

🔄 7. EF Core Migrations Workflow

  1. Define or Modify your POCO classes in DomainClasses/.
  2. Run:
    dotnet ef migrations add UpdateEmployeeTable
    
  3. EF Core creates two files:
    • 20220509142849_UpdateEmployeeTable.cs (Up/Down methods)
    • 20220509142849_UpdateEmployeeTable.Designer.cs (model snapshot)
  4. Apply migration:
    dotnet ef database update
    

🚀 8. Usage Examples

Apply at Startup:

using var context = new IrbyContext();
context.Database.Migrate();

Inspect Model:
The snapshot helps dotnet ef migrations script generate SQL.


🔍 9. Best Practices & Maintenance

  • Do not hand‐edit designer files—they’re auto‐generated.
  • Keep model classes (DomainClasses/*) in sync; regenerate migrations for schema changes.
  • Use descriptive migration names: e.g., AddEmployeeStatus instead of generic “UpdateEmployeeTable.”

🎯 10. Conclusion

This Designer file is the complete blueprint of your EF Core model as of the UpdateEmployeeTable migration. It ensures EF Core knows exactly how your database schema should look—what tables exist, which columns & types they have, how they relate via FKs, and how inheritance is mapped. Keep your domain classes correct, rely on migrations to evolve your schema, and let EF Core handle the heavy lifting! 🚀


💡 Tip: Always review the generated Up/Down methods in the main migration file; they show the actual DDL changes being applied to the database.