πŸ“‘ Table of Contents

  1. Overview & Purpose
  2. Migration Context
  3. Dependencies & Technologies
  4. The BuildTargetModel Method
  5. Entities & Data Models
    5.1. Key Entities Summary
    5.2. Asset Entity & New Columns πŸ†•
  6. Relationships & Navigation Properties
  7. Table Mappings & Inheritance (TPH)
  8. Usage Examples & EF Core Commands πŸ’»
  9. Sample UML Class Diagram πŸ“Š
  10. Conclusion 🏁

1. Overview & Purpose

This auto-generated C# file is part of the Entity Framework Core (EF Core) migration named:

20250319224535_AddColumnsToAssetTable

It contains a snapshot of your model configuration after applying the migration. EF Core uses this snapshot to:

  • Compare model changes between migrations
  • Scaffold Up/ Down scripts
  • Generate SQL DDL to keep your database schema in sync

πŸ€– Note: You should not manually edit this file; it is maintained by EF Core.


2. Migration Context

  • Migration Id: 20250319224535_AddColumnsToAssetTable
  • DbContext: IrbyContext
  • Purpose: Add new columns to the Asset table and reflect those changes in the EF Core model.

3. Dependencies & Technologies

Technology Version / Usage
.NET / C# 9.0.0+
Entity Framework Core Microsoft.EntityFrameworkCore & Migrations
Database Provider SQL Server (UseIdentityColumns)
Tooling dotnet ef migrations

4. The BuildTargetModel Method

Within the partial class AddColumnsToAssetTable, you’ll find:

protected override void BuildTargetModel(ModelBuilder modelBuilder) { /* … */ }
  • Purpose: Rebuild the EF Core model after migration
  • Key APIs:
    • modelBuilder.HasAnnotation(...)
    • Fluent configuration via modelBuilder.Entity<TEntity>(b => { ... })
    • SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder)

πŸ› οΈ All table, column, index, key and relationship configurations live here.


5. Entities & Data Models

5.1. Key Entities Summary

Below is a subset of tables configured in this snapshot. The project has over 70 entities; we focus on the most relevant:

Entity Class Table (Schema) Key Property
AccountBalances irby.AccountBalances AccountBalanceId (GUID)
Asset irby.Asset Id (GUID)
AssetHistory irby.AssetHistory Id (GUID)
AssetModel irby.AssetModel Id (int)
AssetModelDetail AssetModelDetail Id (int)
FieldOffice irby.FieldOffice Id (GUID)
Employee irby.Employee EmployeeId (GUID)
…and many more…

5.2. Asset Entity & New Columns πŸ†•

The core of this migration is the Asset table. Here's how EF Core defines it after adding new columns:

Column Name CLR Type Constraints
Id Guid PK, .ValueGeneratedOnAdd()
Category string IsRequired()
ComplianceStatus string (New)
FieldOfficeId Guid? FK to FieldOffice
LastCheckIn DateTime (New)
ModelId int FK to AssetModel
NeedsReview bool (New)
Notes string (New)
OwnerId Guid? FK to Employee
PrimaryUPN string (New)
PurchaseDate DateTimeOffset (New)
SerialNumber string (New)
Status string IsRequired()
Discriminator string (length 21) For TPH inheritance (e.g., Computer, Dock, MobileDevice…)
modelBuilder.Entity("IrbyImaging.DomainClasses.Asset", b =>
{
    b.Property<Guid>("Id")
     .ValueGeneratedOnAdd()
     .HasColumnType("uniqueidentifier");

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

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

    b.Property<Guid?>("FieldOfficeId")
     .HasColumnType("uniqueidentifier");

    b.Property<DateTime>("LastCheckIn")
     .HasColumnType("datetime2");

    b.Property<int>("ModelId")
     .HasColumnType("int");

    b.Property<bool>("NeedsReview")
     .HasColumnType("bit");

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

    b.Property<Guid?>("OwnerId")
     .HasColumnType("uniqueidentifier");

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

    b.Property<DateTimeOffset>("PurchaseDate")
     .HasColumnType("datetimeoffset");

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

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

    b.HasKey("Id");

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

    b.ToTable("Asset", "irby");
    b.HasDiscriminator().HasValue("Asset");
    b.UseTphMappingStrategy();
});

🎯 Impact: This migration extends the Asset entity with audit & operational columns to track ownership, location, review status, and purchase metadata.


6. Relationships & Navigation Properties

At the bottom of BuildTargetModel, EF Core wires up all foreign keys and navigations:

modelBuilder.Entity("IrbyImaging.DomainClasses.Asset", b =>
{
    b.HasOne("IrbyImaging.DomainClasses.FieldOffice", "FieldOffice")
     .WithMany("Assets")
     .HasForeignKey("FieldOfficeId");

    b.HasOne("IrbyImaging.DomainClasses.AssetModel", "Model")
     .WithMany()
     .HasForeignKey("ModelId")
     .OnDelete(DeleteBehavior.Cascade)
     .IsRequired();

    b.HasOne("IrbyImaging.DomainClasses.Employee", "Owner")
     .WithMany("Assets")
     .HasForeignKey("OwnerId");
});
  • Cascade Deletes: Removing an AssetModel will delete related Asset entries
  • Optional FKs: FieldOfficeId & OwnerId are nullable

7. Table Mappings & Inheritance (TPH)

EF Core uses Table-Per-Hierarchy (TPH) for the Asset family:

CLR Type Discriminator Value
Asset "Asset"
Computer "Computer"
Dock "Dock"
MobileDevice "MobileDevice"
Monitor "Monitor"
NetworkEquipment "NetworkEquipment"
OfficePrinter "OfficePrinter"
Printer "Printer"
SafetyEquipment "SafetyEquipment"
b.ToTable("Asset", "irby");
b.HasDiscriminator()
 .HasValue("Asset");

8. Usage Examples & EF Core Commands πŸ’»

To create and apply this migration in your project:

# 1. Scaffold a new migration
dotnet ef migrations add AddColumnsToAssetTable

# 2. Apply to the database
dotnet ef database update
  • The .Designer.cs file holds the model snapshot.
  • The accompanying <Migration>.cs contains the Up()/Down() steps.

9. Sample UML Class Diagram πŸ“Š

Below is a simplified Mermaid diagram focusing on Asset, AssetModel, FieldOffice & Employee:

classDiagram
    class Asset {
        +Guid Id
        +string Category
        +string ComplianceStatus
        +DateTime LastCheckIn
        +DateTimeOffset PurchaseDate
        +bool NeedsReview
        +string Notes
        +string SerialNumber
        +string Status
    }
    class AssetModel {
        +int Id
        +string Name
        +string Category
    }
    class FieldOffice {
        +Guid Id
        +string Name
    }
    class Employee {
        +Guid EmployeeId
        +string FirstName
        +string LastName
    }

    Asset --> AssetModel : ModelId (FK)
    Asset --> FieldOffice : FieldOfficeId (FK)
    Asset --> Employee : OwnerId (FK)

10. Conclusion 🏁

The AddColumnsToAssetTable.Designer.cs file is a critical piece of your EF Core migration pipeline. It:

  • Reflects the post-migration model
  • Drives change detection in subsequent migrations
  • Maps CLR classes to database artifacts (tables, columns, keys, relationships)

⚠️ Reminder: This file is auto-generated. Any manual edits will be overwritten by EF Core tooling. Always modify your C# domain classes or use the dotnet ef migrations commands to introduce schema changes.

Happy coding! πŸš€