πŸ“¦ IrbyImaging.DbContext.Migrations

20221103025134_DeleteColumnCustomerTable.Designer.cs Documentation


πŸ“‹ Table of Contents

  1. 🎯 Overview & Purpose
  2. πŸ› οΈ Dependencies & Technologies
  3. πŸ”‘ Key Functional Areas
  4. πŸ“¦ Data Models (Entities) & Mappings
  5. 🧩 Relationships & Constraints
  6. πŸ—„οΈ Migrations & Versioning
  7. πŸš€ Usage Example
  8. πŸ“š Conclusion

🎯 1. Overview & Purpose

This file is an EF Core Migrations Designerβ€”an auto‑generated snapshot of your Entity Framework Core model at a given point in time.

  • Filename: 20221103025134_DeleteColumnCustomerTable.Designer.cs
  • Namespace: IrbyImaging.DbContext.Migrations
  • Context: IrbyContext
  • Migration: DeleteColumnCustomerTable

Why it exists
When you add or remove properties on your domain classes and run dotnet ef migrations add, EF Core creates two files:

  • A migration file (Up/Down methods to apply/revert changes)
  • A designer file (this file), which captures the BuildTargetModel, i.e. the final state of the EF model after applying the migration.

πŸ› οΈ 2. Dependencies & Technologies

Technology Purpose
.NET 5.0 Runtime & Base Class Library
EntityFrameworkCore ORM for .NET, model building & migrations
C# Language of the generated code
SQL Server Underlying relational database

Key using statements:

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

πŸ”‘ 3. Key Functional Areas

  1. BuildTargetModel
    • Central method that describes all entities, their properties, table mappings, indexes, keys, and relationships.
  2. Entity Definitions
    • For each domain class (Asset, Customer, Employee, …), the code uses modelBuilder.Entity(...) to set up columns and constraints.
  3. Discriminator Setup
    • Supports table-per-hierarchy (TPH) for the Asset class hierarchy (Computer, DeskPhone, MobileDevice, …).
  4. Navigation & Relationships
    • Defines foreign‑key relationships and navigation properties to enforce referential integrity.

πŸ“¦ 4. Data Models (Entities) & Mappings

Below is a sample of how EF Core maps a domain class to database schema. This designer file contains 38+ entities. We highlight a few key ones:

πŸ”Ή Customer Entity

Property Type Attributes
Id Guid PK, ValueGeneratedOnAdd
CustomerNumber string nvarchar(max), nullable
Name string nvarchar(max), nullable
modelBuilder.Entity("IrbyImaging.DomainClasses.Customer", b =>
{
    b.Property<Guid>("Id")
        .ValueGeneratedOnAdd()
        .HasColumnType("uniqueidentifier");
    b.Property<string>("CustomerNumber")
        .HasColumnType("nvarchar(max)");
    b.Property<string>("Name")
        .HasColumnType("nvarchar(max)");
    b.HasKey("Id");
    b.ToTable("Customer", "irby");
});

πŸ”Ή Asset Entity Hierarchy

Base Type Table Discriminator Derived Types
Asset irby.Asset Discriminator Computer, DeskPhone, Dock, MobileDevice, Monitor, NetworkEquipment, …
// Base mapping
modelBuilder.Entity("IrbyImaging.DomainClasses.Asset", b =>
{
    b.HasKey("Id");
    b.ToTable("Asset", "irby");
    b.Property<string>("Discriminator")
     .IsRequired()
     .HasColumnType("nvarchar(max)");
    b.HasDiscriminator<string>("Discriminator")
     .HasValue("Asset");
});

// Sub-class example
modelBuilder.Entity("IrbyImaging.DomainClasses.Computer", b =>
{
    b.HasBaseType("IrbyImaging.DomainClasses.Asset");
    b.Property<string>("AssetNumber")
     .HasColumnName("AssetNumber");
    b.HasDiscriminator()
     .HasValue("Computer");
});

🧩 5. Relationships & Constraints

πŸ“œ Foreign Key & Navigation Setup

  • Asset β†’ AssetModel (Required, Cascade delete)
  • Asset β†’ FieldOffice, Asset β†’ Employee (Optional)
  • Employee β†’ Job (Required, Cascade delete)
  • FieldOffice β†’ Employee (Manager/PM/Superintendent) (Optional)
modelBuilder.Entity("IrbyImaging.DomainClasses.Asset", b =>
{
    b.HasOne("IrbyImaging.DomainClasses.AssetModel", "Model")
     .WithMany()
     .HasForeignKey("ModelId")
     .OnDelete(DeleteBehavior.Cascade)
     .IsRequired();
    // ...
});

🌐 Mermaid ER Diagram (excerpt)

erDiagram
    ASSET ||--o{ ASSET_MODEL : Model
    ASSET }o--|| FIELDOFFICE : FieldOffice
    EMPLOYEE }o--|| JOB : CurrentJob
    FIELDOFFICE }o--|| EMPLOYEE : Manager

πŸ—„οΈ 6. Migrations & Versioning

  • Migration Name
    DeleteColumnCustomerTable
  • Designer File Role
    • Captures the target state (post-migration) of the EF Core model.
    • Does not contain the Up() / Down() logic (that’s in the other file: 20221103025134_DeleteColumnCustomerTable.cs).

Tip: When you run dotnet ef migrations add DeleteColumnCustomerTable, EF compares your current model to the last snapshot, generates an Up/Down script, then updates the snapshot (this file).


πŸš€ 7. Usage Example

1. Apply the Migration

cd /path/to/IrbyImaging.DbContext
dotnet ef database update DeleteColumnCustomerTable

2. Verify Schema Change

Use SQL Server Management Studio or Azure Data Studio to confirm the Customer table no longer contains the deleted column.

3. Rollback (if needed)

dotnet ef database update [PreviousMigrationName]

πŸ“š 8. Conclusion

This designer file is a critical part of EF Core’s migration pipeline:

  • 🎨 Snapshot of your model at point-in-time.
  • πŸ”„ Feeds EF when generating future migrations.
  • ⚠️ Do not modify manuallyβ€”it’s auto‑generated.

Understanding this file helps you:

  • Troubleshoot model drift.
  • Review how EF sees your domain classes.
  • Ensure your database schema matches your C# model.

Happy coding! πŸŽ‰


Generated by Entity Framework Core 5.0.12