πŸ“š Migration Documentation: 20240124164811_MoreUpdatesToEmployeeTable.Designer.cs


Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Migration Anatomy
  4. Key Entity Mappings
  5. Relationships & Constraints
  6. Using this Migration
  7. Sample UML Diagram
  8. Conclusion

1. Overview & Purpose

πŸš€ 20240124164811_MoreUpdatesToEmployeeTable.Designer.cs is an auto-generated Entity Framework Core snapshot file. It captures the target model state after the migration MoreUpdatesToEmployeeTable is applied.

  • EF Core uses this to compute diffs when you add future migrations.
  • It reflects changes such as new column types, indexes, and foreign keys.

2. Dependencies & Technologies

Technology / Namespace Version / Purpose
Microsoft.EntityFrameworkCore Core ORM library
Microsoft.EntityFrameworkCore.Migrations Migration API
Microsoft.EntityFrameworkCore.Metadata Model building
Microsoft.EntityFrameworkCore.Infrastructure Infrastructure for design-time services
Microsoft.EntityFrameworkCore.Storage.ValueConversion Column value conversions
.NET 7.0 Target framework
SQL Server (Relational) Database provider (UseIdentityColumns, bit, nvarchar, etc.)

3. Migration Anatomy

Attributes & Metadata

[DbContext(typeof(IrbyContext))]
[Migration("20240124164811_MoreUpdatesToEmployeeTable")]
partial class MoreUpdatesToEmployeeTable
{
    protected override void BuildTargetModel(ModelBuilder modelBuilder) { ... }
}
  • [DbContext] indicates the IrbyContext used.
  • [Migration("...")] ties this snapshot to the "20240124164811_MoreUpdatesToEmployeeTable" migration identifier.

BuildTargetModel Method

This method reconstructs the entire EF Core model schema at this migration point:

protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
    modelBuilder
      .HasAnnotation("ProductVersion", "7.0.7")
      .HasAnnotation("Relational:MaxIdentifierLength", 128);

    // Use SQL Server identity columns globally
    SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);

    // Entity configurations follow...
}
  • Annotations: EF version & identifier length.
  • UseIdentityColumns: Configures defaults for SQL Server primary keys.

4. Key Entity Mappings

The snapshot configures dozens of entities under the irby schema. Below we highlight the most critical updates.

Employee Table Updates πŸ“‹

Property Type Constraints
EmployeeId uniqueidentifier Primary Key, value generated on add
AzureUserId uniqueidentifier
JdeNumber nvarchar(450) Unique Index, filtered NOT NULL
DepartmentId uniqueidentifier? Foreign Key β†’ Department
JobId uniqueidentifier FK β†’ Job (Required, Cascade on delete)
RoleId uniqueidentifier? FK β†’ Role
Status nvarchar(max) Required
... ... ...

πŸ”‘ Indices on DepartmentId, JobId, RoleId, and a unique index on JdeNumber:

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

πŸ’‘ Purpose of Updates

  • Restrict JdeNumber length to 450 characters to support indexing.
  • Ensure uniqueness of JdeNumber across employees.
  • Link employees to Departments, Jobs, and Roles.

Asset Inheritance (TPH) πŸ–₯οΈπŸ“±

The migration uses Table-Per-Hierarchy for Asset and subclasses:

modelBuilder.Entity("IrbyImaging.DomainClasses.Asset", b => { ... })
    .HasDiscriminator<string>("Discriminator")
    .HasValue("Asset")
    .UseTphMappingStrategy();

Subclasses include:

  • Computer
  • DeskPhone
  • MobileDevice
  • Monitor
  • OfficePrinter
  • SafetyEquipment, etc.

Each discriminator value maps back to the same "irby.Asset" table, with additional columns as needed.


5. Relationships & Constraints πŸ”—

  • One-to-Many:

    • Department β†’ Employee
    • Job β†’ Employee
    • Employee β†’ Asset
    • Ticket β†’ Update
  • Many-to-One:

    • Employee references Role
    • LinkUserRole maps Employee ↔ Role
  • Cascade Deletes:

    • Deleting a Job cascades to its Employees.
    • Deleting a Ticket cascades to its Updates.

6. Using this Migration

Generating SQL Scripts

To see the SQL that EF Core would run:

dotnet ef migrations script \
  20240124164810_PreviousMigration \
  20240124164811_MoreUpdatesToEmployeeTable \
  --output UpdateEmployeeTable.sql

Applying to Database

dotnet ef database update 20240124164811_MoreUpdatesToEmployeeTable

Or update all pending:

dotnet ef database update

7. Sample UML Diagram 🎨

classDiagram
    Employee "1" o-- "0..1" Department : DepartmentId
    Employee "1" o-- "1" Job        : JobId
    Employee "1" o-- "0..1" Role     : RoleId
    Employee "1" o-- "*" Asset     : OwnerId

    class Employee {
      +Guid EmployeeId
      +Guid AzureUserId
      +string JdeNumber
      +string FirstName
      +string LastName
      +string Status
      +Guid? DepartmentId
      +Guid RoleId
      +Guid JobId
      +DateTime? TerminatedDate
      +...
    }

8. Conclusion

βœ… This Designer snapshot is crucial for EF Core to reconcile your model with the database. It:

  • Captures schema definitions, indexes, and relationships
  • Ensures future migrations detect only incremental changes
  • Provides a single source of truth for your ORM mapping

Keep it under source control, but do not manually edit.
Future changes to your domain classes or OnModelCreating will produce new snapshots.


β€œIn EF Core, design-time snapshots like this help EF understand the β€˜before’ state of your model so it can generate precise DDL diff scripts for your database.” πŸš€


β€” End of Migration Documentation