📘 EF Core Migration Designer Documentation
File: IrbyImaging.DbContext/Migrations/20240318221107_UpdateLegalDashDBtables.Designer.cs
📑 Table of Contents
- Overview & Purpose
- File Structure & Key Sections
- Dependencies & Technologies
- BuildTargetModel Method
- How EF Migrations Use This File
- Usage Examples
- UML Diagram (High‑Level)
- Conclusion
Overview & Purpose
What is this file?
- This is an auto‑generated designer file created by Entity Framework Core when you add or update a migration (
UpdateLegalDashDBtables). - It defines the target model snapshot: all entities, properties, tables and relationships as they should exist after applying this migration.
- Do not modify this file manually; EF Core regenerates it to track your model’s state.
Why does it exist?
- To enable EF Core to calculate the Up and Down steps when you run
dotnet ef migrations addordotnet ef database update. - To serve as a model snapshot, so future migrations know what changed.
File Structure & Key Sections
namespace IrbyImaging.DbContext.Migrations
{
[DbContext(typeof(IrbyContext))]
[Migration("20240318221107_UpdateLegalDashDBtables")]
partial class UpdateLegalDashDBtables
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
// 1. Global annotations & conventions
// 2. Entity<Type> configurations
// 3. Relationship (HasOne/WithMany) mappings
}
}
}
-
Attributes
[DbContext(typeof(IrbyContext))]– ties it to yourIrbyContext.[Migration("20240318221107_UpdateLegalDashDBtables")]– unique timestamped ID.
-
BuildTargetModel
- Core method where ModelBuilder is used to configure every table, column, PK, FK, index, discriminator, etc.
Dependencies & Technologies
| Dependency | Version | Purpose |
|---|---|---|
| Microsoft.EntityFrameworkCore | 7.0.7 | ORM for model → relational schema mapping |
| Microsoft.EntityFrameworkCore.Design | 7.0.x | Tools for scaffolding migrations & DB updates |
| SqlServerModelBuilderExtensions | – | SQL Server–specific conventions (IDENTITY columns) |
- Target Database: SQL Server (uses
uniqueidentifier,nvarchar(max),datetime2, etc.) - Language: C#
- Nullable Context: Disabled (
#nullable disable)
BuildTargetModel Method
This is the heart of the designer file.
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.7")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
// Use SQL Server IDENTITY columns
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
// --- Entity configurations below ---
modelBuilder.Entity("IrbyImaging.DomainClasses.Asset", b => { ... });
modelBuilder.Entity("IrbyImaging.DomainClasses.Employee", b => { ... });
// ...
// then relationship mappings
modelBuilder.Entity("IrbyImaging.DomainClasses.Asset", b => { b.HasOne(...); });
// ...
#pragma warning restore 612, 618
}
4.1 Annotations & Conventions ✨
- ProductVersion: EF Core version that generated it.
- MaxIdentifierLength: Maximum SQL identifier length.
- UseIdentityColumns: Globally configures all integer PKs to use
IDENTITY.
4.2 Entity Configuration Highlights 📋
Below is a sample summary. The migration actually configures ~80 domain entities.
| Entity Class | Table (Schema) | Primary Key | Notes / Discriminators |
|---|---|---|---|
| Asset | irby.Asset |
Id (uniqueidentifier) |
TPH with Discriminator: Asset, Computer, MobileDevice, etc. |
| AssetHistory | irby.AssetHistory |
Id |
Tracks asset changes |
| Employee | irby.Employee |
EmployeeId |
FK → Department, Job, Role |
| Job | irby.Job |
JobId |
FK → FieldOffice |
| FieldOffice | irby.FieldOffice |
Id |
Multiple employee FKs |
| DocumentImage | irby.DocumentImage |
DocumentImageId |
Legal/Safety/IT/HR flags |
| Ticket & Update | irby.Ticket / irby.Update |
Id / Id |
One Ticket → many Updates |
| Permission & Role Links | irby.LinkRolePermission |
Id |
Many-to-many via linking tables |
| TimeSheetEntry & TimeEntryType | irby.TimeSheetEntry |
TimeSheetEntryId |
Tracks work hours |
Tip: Scroll through code to see
.Property(...),.HasKey(),.HasIndex(),.HasOne/WithMany().
4.3 Relationship Mapping 🔗
At the bottom of BuildTargetModel, each entity’s FKs and navigations are wired up:
modelBuilder.Entity("IrbyImaging.DomainClasses.Asset", b =>
{
b.HasOne("IrbyImaging.DomainClasses.FieldOffice", "FieldOffice")
.WithMany("Assets")
.HasForeignKey("FieldOfficeId");
b.HasOne("IrbyImaging.DomainClasses.Employee", "Owner")
.WithMany("Assets")
.HasForeignKey("OwnerId");
b.HasOne("IrbyImaging.DomainClasses.AssetModel", "Model")
.WithMany()
.HasForeignKey("ModelId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
- HasOne/WithMany: Defines 1:N relationships.
- OnDelete(DeleteBehavior.Cascade): Cascade delete behavior.
How EF Migrations Use This File
- Scaffolding: When you run
EF Core snapshots your current model into adotnet ef migrations add UpdateLegalDashDBtables.Designer.cs. - Database Update:
Leverages thedotnet ef database updateUp()method (in the non-designer.cs) to apply schema changes. The designer file represents the post‑migration model.
Usage Examples 💻
-
Add a Migration
dotnet ef migrations add UpdateLegalDashDBtables -
Review Changes
- Inspect Up / Down methods in
20240318221107_UpdateLegalDashDBtables.cs. - Verify the snapshot in the
.Designer.cs.
- Inspect Up / Down methods in
-
Apply Migration
dotnet ef database update
UML Diagram (High‑Level) ✍️
Below is a simplified PlantUML snippet illustrating a few core entities:
@startuml
class Employee {
+ Guid EmployeeId
+ string FirstName
+ string LastName
}
class FieldOffice {
+ Guid Id
+ string Name
}
class Asset {
+ Guid Id
+ string Category
}
Employee "1" -- "0..*" Asset : Owner
FieldOffice "1" -- "0..*" Asset : FieldOffice
AssetModel "1" -- "0..*" Asset : Model
@enduml
Render with any PlantUML-compatible tool to visualize!
Conclusion ✅
This Designer file is a critical part of EF Core’s migration infrastructure. It captures your domain model’s shape in code, enabling robust, repeatable schema evolution.
- Do not edit – it’s auto‑generated.
- Use EF Core CLI or Package Manager Console to scaffold and apply migrations.
- Inspect
.Designer.csto confirm your EF Core model matches your intentions.
Happy coding! 🚀