π Migration Documentation: 20240124164811_MoreUpdatesToEmployeeTable.Designer.cs
Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Migration Anatomy
- Key Entity Mappings
- Relationships & Constraints
- Using this Migration
- Sample UML Diagram
- 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 theIrbyContextused.[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
JdeNumberlength to 450 characters to support indexing. - Ensure uniqueness of
JdeNumberacross 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:
ComputerDeskPhoneMobileDeviceMonitorOfficePrinterSafetyEquipment, etc.
Each discriminator value maps back to the same "irby.Asset" table, with additional columns as needed.
5. Relationships & Constraints π
-
One-to-Many:
DepartmentβEmployeeJobβEmployeeEmployeeβAssetTicketβUpdate
-
Many-to-One:
EmployeereferencesRoleLinkUserRolemapsEmployeeβ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