π Migration Designer Documentation
File: IrbyImaging.DbContext/Migrations/20240130145814_MoreUpdatesToJobTable4.Designer.cs
π Table of Contents
- π Overview & Purpose
- π οΈ Dependencies & Technologies
- π File Structure & Naming Conventions
- ποΈ The BuildTargetModel Method
- π Key Entity: Job
- π Domain Model at a Glance
- π How EF Core Uses This Designer File
- π Applying & Reverting the Migration
- π Sample Code Snippets
- π UML Class Diagram (Excerpt)
- β Conclusion
π Overview & Purpose
When you run dotnet ef migrations add MoreUpdatesToJobTable4, Entity Framework Core scaffolds two files:
- A migration file containing
Up()/Down()methods (to apply or rollback changes). - A designer file (this one), which contains a snapshot of the entire EF Core model after the migration.
Why is this important?
The designer file ensures EF Core knows the complete model state. On subsequent migrations, EF Core diffs the current model against this snapshot to generate the next migration.
π οΈ Dependencies & Technologies
| Dependency | Version / Notes |
|---|---|
| Microsoft.EntityFrameworkCore | 7.0.7 |
| Microsoft.EntityFrameworkCore.SqlServer | Uses UseIdentityColumns() |
| Microsoft.EntityFrameworkCore.Metadata | ModelBuilder API |
| C# / .NET 7 | Target framework for code generation |
π File Structure & Naming Conventions
IrbyImaging.DbContext
βββ Migrations
βββ 20240130145814_MoreUpdatesToJobTable4.cs # Migration operations
βββ 20240130145814_MoreUpdatesToJobTable4.Designer.cs # Model snapshot
- Timestamp prefix (
20240130145814_) keeps migrations ordered. MoreUpdatesToJobTable4: descriptive name of the change set..Designer.cs: auto-generated, do not edit manually.
ποΈ The BuildTargetModel Method
At the heart of this file is:
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
modelBuilder
.HasAnnotation("ProductVersion", "7.0.7")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
// ... Hundreds of modelBuilder.Entity<>() calls ...
}
- Purpose: Describes every entity, property, key, index, table name, discriminator, and relationship in your DbContext after this migration.
- Why? EF Core uses it to compute the delta when you add the next migration.
π Key Entity: Job
This migrationβs name suggests changes around the Job table. Inspecting the snapshot:
modelBuilder.Entity("IrbyImaging.DomainClasses.Job", b =>
{
b.Property<Guid>("JobId")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
b.Property<string>("JdeNumber")
.HasColumnType("nvarchar(450)");
// ... other properties ...
b.HasKey("JobId");
// π Here's the newly added unique index on JdeNumber:
b.HasIndex("JdeNumber")
.IsUnique()
.HasFilter("[JdeNumber] IS NOT NULL");
b.ToTable("Job", "irby");
});
What changed?
JdeNumberconfigured asnvarchar(450)(instead ofnvarchar(max)), making it indexable.- A unique index ensures no two Jobs share the same JDE number.
π Domain Model at a Glance
This snapshot lists all entities. Weβll group them by subdomain:
| Subdomain | Entities |
|---|---|
| Asset Management | Asset (TPH), AssetHistory, AssetModel, AssetModelDetail, Manufacturer, OperatingSystem, Computer, MobileDevice, β¦ |
| Document Imaging | DocumentImage, DocumentType, DocumentCategory, DocumentAttribute, BlobData, TempDocumentData, IPMMigration, IrbyForms |
| People & Roles | Employee, Department, Role, Permissions, LinkUserRole, LinkRolePermission |
| Jobs & Offices | Job, FieldOffice, JobAndFieldOfficeMapping, ContractsByLead, DirectorProperties, β¦ |
| Billing & Finance | BillingInvoice, GeneralLedger, GLTypeDetail, CarrierType, CarrierTypeDetail |
| Audit & Reports | AuditDetail, AuditReport, LicenseReport, ODSCustomer, ODSEmployee, ODSJob |
| Time & Tickets | TimeEntryType, TimeSheetEntry, Ticket, Update, Issue |
Note: Some tables (e.g.,
ContractTypeDetails,DirectorLeaseInfo) serve reporting or readβonly views.
π How EF Core Uses This Designer File
- Snapshot: Records the last known model state for migrations.
- Diffing: When you run
Add-Migration, EF Core loads:- Your current
DbContextmodel (via data annotations & Fluent API). - The snapshot in the
.Designer.cs. - Computes changes β generates
Up()/Down()code.
- Your current
- Model Validation: At runtime, EF Core ensures the target DB matches the snapshot before applying
Up().
π Applying & Reverting the Migration
# Apply migrations up to this point
dotnet ef database update
# To rollback this specific migration
dotnet ef database update 20240130145813_PreviousMigration
Tip: Always commit both
.csand.Designer.csto source control together.
π Sample Code Snippets
1. Unique Index on JdeNumber
b.Property<string>("JdeNumber")
.HasColumnType("nvarchar(450)");
b.HasIndex("JdeNumber")
.IsUnique()
.HasFilter("[JdeNumber] IS NOT NULL");
2. TableβPerβHierarchy (TPH) for Asset
modelBuilder.Entity("IrbyImaging.DomainClasses.Asset", b =>
{
b.HasDiscriminator<string>("Discriminator")
.HasValue("Asset");
// Derived types share the same table
b.UseTphMappingStrategy();
});
3. Relationships Example (Job β FieldOffice)
modelBuilder.Entity("IrbyImaging.DomainClasses.Job")
.HasOne("IrbyImaging.DomainClasses.FieldOffice", null)
.WithMany("Jobs")
.HasForeignKey("FieldOfficeId");
π UML Class Diagram (Excerpt)
@startuml
class Job {
+ Guid JobId
+ string Name
+ string JdeNumber
+ int Status
}
class FieldOffice {
+ Guid Id
+ string Name
+ string JdeNumber
}
Job "N" --> "1" FieldOffice : FieldOfficeId
@enduml
Above: Job holds a foreign key to FieldOffice.
β Conclusion
This designer file is a complete snapshot of your EF Core model after the MoreUpdatesToJobTable4 migration.
- Never hand-edit it.
- Itβs crucial for EF Coreβs diffing algorithm.
- Shows how every entity, property, key, index, and relationship is configured.
By understanding the BuildTargetModel method and its contents, you gain insight into your database schema as EF Core sees itβempowering you to troubleshoot migrations, optimize queries, and maintain a clean data layer.
Happy coding! π