IrbyImaging.DbContext.Migrations

File: 20220302171015_NewRefTables.Designer.cs


Table of Contents

  1. Introduction 🚀
  2. Overview & Purpose 🔍
  3. Dependencies & Technologies ⚙️
  4. Key Functional Areas 🗂️
  5. Data Model Entities 🗄️
  6. Schema Definitions & Mappings 📜
  7. Entity Relationships 🔗
  8. Generating & Applying the Migration 🛠️
  9. Usage Examples 💡
  10. Conclusion & Best Practices 🏁

1. Introduction 🚀

This file is an auto‑generated designer for the Entity Framework Core migration named NewRefTables. It captures the target snapshot of your model—table names, column types, keys, indexes, inheritance discriminators, and relationships—so EF Core can compare it to your database and generate the appropriate SQL to bring them in sync.


2. Overview & Purpose 🔍

  • Location
    IrbyImaging.DbContext/Migrations/20220302171015_NewRefTables.Designer.cs
  • Purpose
    • Defines all tables under the "irby" schema (plus a few staging tables).
    • Configures columns, keys, indexes, discriminators (for TPH inheritance).
    • Establishes foreign‑key relationships and cascading behaviors.
    • Serves as the model snapshot for EF Core migrations.

3. Dependencies & Technologies ⚙️

Package Version Role
Microsoft.EntityFrameworkCore 5.0.12 Core ORM
Microsoft.EntityFrameworkCore.SqlServer 5.0.12 SQL Server provider
Microsoft.EntityFrameworkCore.Design 5.0.12 Design‑time services (migrations, scaffolding)
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

4. Key Functional Areas 🗂️

  1. Model Snapshot
    • protected override void BuildTargetModel(ModelBuilder modelBuilder)
    • Captures the EF Core model state after this migration.
  2. Table & Column Configuration
    • modelBuilder.Entity("Namespace.ClassName", b => { … })
    • .Property<…>(), .HasColumnType(), .IsRequired(), .HasAnnotation().
  3. Inheritance (TPH)
    • Single table irby.Asset with a Discriminator column.
    • Derived types like Computer, MobileDevice, DeskPhone, etc.
  4. Indexes & Constraints
    • Unique filters on JdeNumber, Imei, etc.
    • Foreign keys with OnDelete(DeleteBehavior.Cascade).

5. Data Model Entities 🗄️

Below is a summary of the primary entities configured:

Entity Class Table Name PK Notes / Inheritance
Asset irby.Asset Id (GUID) TPH root, Discriminator → (“Asset”, “Computer”, …)
AssetHistory irby.AssetHistory Id (GUID) Audit log
AssetModel irby.AssetModel Id (int) FK → AssetManufacturer
AssetModelDetail AssetModelDetail Id (int) Optional FK → AssetModel
CarrierType irby.CarrierType Id (GUID) Parent for CarrierTypeDetail
CarrierTypeDetail CarrierTypeDetail Id (GUID) FK → CarrierType
CovidTestData irby.CovidTestData CovidTestDataId (GUID) Health data
DocumentImage irby.DocumentImage DocumentImageId (GUID) OCR, binary storage
Employee irby.Employee Id (GUID) FK → Job (CurrentJobId)
FieldOffice irby.FieldOffice Id (GUID) FK → Employee (ManagerId)
GeneralLedger irby.GeneralLedger Id (GUID) Master GL table
GLTypeDetail GLTypeDetail Id (GUID) FK → GeneralLedger
Job irby.Job Id (GUID) Unique on JdeNumber
Manufacturer irby.AssetManufacturer Id (int) Produces AssetModels
ODSEmployee (Staging) irby.ODS_Employee_Staging EMPLOYEE_NUMBER (str) ODS staging
ODSJob (Staging) irby.ODS_Jobs_Staging ORG_BU (str) ODS staging
OperatingSystem irby.OperatingSystem Id (int) OS catalog
TempDocumentData irby.TempDocumentData TempDocumentDataId (GUID) Temp storage
IPMMigration irby.IPMMigration IPMMigrationId (GUID) IM/Migration data

Inheritance (Discriminator)

All the following derive from Asset and share the irby.Asset table:

Discriminator CLR Type
Asset Asset
Computer Computer
DeskPhone DeskPhone
Dock Dock
MobileDevice MobileDevice
Monitor Monitor
NetworkEquipment NetworkEquipment
OfficePrinter OfficePrinter
Printer Printer
SafetyEquipment SafetyEquipment

6. Schema Definitions & Mappings 📜

Below is an example excerpt showing how EF Core configures the Asset entity:

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

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

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

  b.HasKey("Id");
  b.HasIndex("FieldOfficeId");
  b.HasIndex("ModelId");
  b.HasIndex("OwnerId");

  b.ToTable("Asset", "irby");

  // TPH discriminator: root value
  b.HasDiscriminator<string>("Discriminator")
   .HasValue("Asset");
});
  • .ToTable("Asset", "irby")
    Places the table in the irby schema.
  • Indexes
    .HasIndex("ModelId") creates a non‑unique FK index.
  • Discriminator
    Enables Table‑Per‑Hierarchy (TPH) inheritance.

7. Entity Relationships 🔗

Below is a simplified UML diagram highlighting the primary relationships:

  +----------------+       1    *      +------------+ 
  |  FieldOffice   |◄───────────────┐  |    Asset   |
  +----------------+                │  +------------+  
      1   *                         │  | Id         |
  [Manager]─┐                       └─▶| FieldOfficeId [FK]
            │                          | ModelId   [FK]
            │                          | OwnerId   [FK]
            │                          +------------+
            │
            └── manages ─ ─ ─ 1       * [Employee]      
                                   
                 ┌───────────────────┐
                 │      Job          │
                 └───────────────────┘
                 * 1
            [Employee.CurrentJobId]
  • Asset → AssetModel (many‑to‑one)
  • AssetModel → Manufacturer (many‑to‑one)
  • Asset → Employee (Owner)
  • Employee → Job (CurrentJob)
  • FieldOffice → Employee (Manager)

8. Generating & Applying the Migration 🛠️

Create new migration

dotnet ef migrations add NewRefTables -c IrbyContext

Apply migrations

dotnet ef database update -c IrbyContext
  • The .Designer.cs file is the model snapshot used by EF Core to detect changes.
  • Never edit this file by hand—regenerate via the CLI or Package Manager Console.

9. Usage Examples 💡

1) Querying All Active Computers

using var ctx = new IrbyContext();
var activeComputers = ctx.Set<IrbyImaging.DomainClasses.Computer>()
                         .Include(c => c.OperatingSystem)
                         .Where(c => c.Status == "Active")
                         .ToList();

2) Adding a New Field Office

var denverOffice = new FieldOffice {
  Name        = "Denver Office",
  JdeNumber   = "100123",
  ManagerId   = someManagerGuid
};
ctx.FieldOffices.Add(denverOffice);
ctx.SaveChanges();

10. Conclusion & Best Practices 🏁

  • Keep your model and migrations in sync by applying migrations frequently.
  • Review the .Designer.cs after each scaffold to verify naming, schemas, indexes.
  • Leverage TPH inheritance carefully; maintain clear discriminator values.
  • Index columns you query often (e.g., JdeNumber, Imei).
  • Avoid manual edits to designer snapshots—rely on EF Core tools.

Happy coding! 🎉