π Documentation: 20240130134140_MoreUpdatesToJobTable.Designer.cs
π Table of Contents
- Overview & Purpose
- EF Core Migrations at a Glance
- File Structure & Attributes
- The BuildTargetModel Method
- Data Models & Entity Mappings
- 5.1. Key Entities
- 5.2. Table & Schema Mapping
- π§ Key Changes to Job Table
- Dependencies & Technologies
- Usage Examples & CLI Commands
- UML Diagram (Simplified)
- Conclusion π―
1. Overview & Purpose
This auto-generated designer file is part of an Entity Framework Core (EF Core) migration named MoreUpdatesToJobTable. It contains a snapshot of the target data model after the migration has been applied. EF Core uses this snapshot to:
- Compare your current model to the database schema.
- Generate SQL commands for future migrations.
- Ensure consistency across development, CI/CD pipelines, and production.
File:
IrbyImaging.DbContext/Migrations/20240130134140_MoreUpdatesToJobTable.Designer.cs
2. EF Core Migrations at a Glance
- π Migrations
Represent version-controlled schema changes. - π Migration Class
DefinesUp()andDown()methods to apply or roll back changes. - π Designer File
Contains theBuildTargetModel(ModelBuilder)method β a complete model snapshot. - βοΈ ModelBuilder
Fluent API for configuring tables, columns, keys, relationships, indexes, etc.
3. File Structure & Attributes
namespace IrbyImaging.DbContext.Migrations
{
[DbContext(typeof(IrbyContext))]
[Migration("20240130134140_MoreUpdatesToJobTable")]
partial class MoreUpdatesToJobTable
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
// β¦ fluent mapping of every entity β¦
}
}
}
[DbContext]associates this migration withIrbyContext.[Migration("...")]assigns a timestamped identifier.BuildTargetModelis auto-generated β do not modify it manually.
4. The BuildTargetModel Method
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.7")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Entity configurations below:
// β’ Properties (Column types, constraints)
// β’ Keys & Indexes
// β’ Relationships (HasOne, WithMany)
// β’ Table & Schema mapping
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
}
Highlights
- Sets provider annotations (SQL Server 7.0.7).
- Enables Identity columns for primary keys.
- Iterates over all entities in
IrbyImaging.DomainClasses.
5. Data Models & Entity Mappings
5.1. Key Entities
There are ~90 entities mapped in this model. Weβll highlight a few central ones:
| Entity Class | Table Name | Schema |
|---|---|---|
Job |
Job |
irby |
FieldOffice |
FieldOffice |
irby |
Employee |
Employee |
irby |
Asset (TPH base) |
Asset |
irby |
DocumentImage |
DocumentImage |
irby |
| β¦ and many more domain-specific types |
5.2. Table & Schema Mapping
modelBuilder.Entity("IrbyImaging.DomainClasses.Job", b =>
{
b.ToTable("Job", "irby");
b.HasKey("JobId");
// Column mappings β¦
});
.ToTable()β maps an entity to a specific table and schema..HasKey()β defines the primary key.- Additional calls configure columns, indexes, and relationships.
6. π§ Key Changes to Job Table
The primary purpose of this migration is captured by updates to the Job entity:
modelBuilder.Entity("IrbyImaging.DomainClasses.Job", b =>
{
b.Property<Guid>("JobId")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
b.Property<string>("Name")
.HasColumnType("nvarchar(max)");
b.Property<string>("JdeNumber")
.HasColumnType("nvarchar(450)");
b.Property<Guid?>("FieldOfficeId")
.HasColumnType("uniqueidentifier");
// β¦ other properties β¦
// Index on FieldOfficeId
b.HasIndex("FieldOfficeId");
// Unique index on JdeNumber (if not null)
b.HasIndex("JdeNumber")
.IsUnique()
.HasFilter("[JdeNumber] IS NOT NULL");
// FK Relationship
b.HasOne("IrbyImaging.DomainClasses.FieldOffice", "FieldOffice")
.WithMany("Jobs")
.HasForeignKey("FieldOfficeId");
});
Summary of Changes
- New/Modified Columns:
JdeNumberconfigured asnvarchar(450)for uniqueness.- Potential addition of new GUID FKs (
ManagerId,ProjectManagerId, etc.).
- Indexes:
- Unique index on
JdeNumber. - Standard index on
FieldOfficeIdfor performance in joins.
- Unique index on
- Relationships:
- Linking
JobβFieldOffice(1 FieldOffice has many Jobs).
- Linking
7. Dependencies & Technologies
| Item | Version / Notes |
|---|---|
| .NET SDK | 7.0.x |
| EF Core | 7.0.7 |
| Database Provider | Microsoft SQL Server |
| C# Language Version | 10.0 (Top-level statements, nullable disabled) |
| Schema & Conventions | TPH (TableβPerβHierarchy) for inheritance on Asset |
8. Usage Examples & CLI Commands
- Add a new migration
dotnet ef migrations add MoreUpdatesToJobTable - Update your database
dotnet ef database update MoreUpdatesToJobTable - Rollback to previous migration
dotnet ef database update <PreviousMigrationName> - Register context in Startup
services.AddDbContext<IrbyContext>(options => options.UseSqlServer(configuration.GetConnectionString("IrbyDb")));
9. UML Diagram (Simplified)
Below is a simplified PlantUML snippet illustrating the core relationship between Job and FieldOffice:
@startuml
class Job {
- Guid JobId
- string Name
- string JdeNumber
- Guid? FieldOfficeId
}
class FieldOffice {
- Guid Id
- string Name
}
FieldOffice "1" -- "0..*" Job : manages >
@enduml
You can paste the above into any PlantUML renderer to visualize the relationship.
10. Conclusion π―
This designer file is a critical piece of your EF Core migration pipeline. It:
- Captures the complete shape of your data model at this migration point.
- Ensures EF Core can diff future changes against the last applied schema.
- Provides fluent mappings for every property, key, index, and relationship.
By understanding its structure and content, developers can better:
- Troubleshoot schema mismatches.
- Extend migrations safely.
- Maintain a clear history of database changes alongside domain model evolution.
Happy coding! π