📄 Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Migration Class Structure
3.1 AddStateTable Migration
3.2 Designer File - BuildTargetModel Method
- Entities & Data Models
5.1 State Entity 🗺️
5.2 Other Entities (Summary) - Relationships & Navigation Properties
- How to Apply the Migration 🚀
- UML Diagram
- Sample Code Snippets
- Conclusion 🔚
1. Overview & Purpose
File
IrbyImaging.DbContext/Migrations/20220804191712_AddStateTable.Designer.cs
This auto-generated designer file is part of an Entity Framework Core migration named AddStateTable. It contains the BuildTargetModel method, which captures the complete EF Core model snapshot after applying the migration. In essence, it tells EF Core:
“This is exactly what the database schema and model should look like at this point in time.”
Key Goals
- 📦 Define all entities, properties, keys, indexes, discriminators, and relationships.
- 🗺️ Introduce a new
Statetable in theirbyschema. - 🤝 Ensure future migrations can diff against this snapshot for changes.
2. Dependencies & Technologies
| Technology / Package | Version | Purpose |
|---|---|---|
| Microsoft.EntityFrameworkCore | 5.0.12 | Core ORM framework |
| Microsoft.EntityFrameworkCore.Migrations | 5.0.12 | Migration API |
| Microsoft.EntityFrameworkCore.SqlServer | 5.0.12 | SQL Server provider |
| .NET 5.0 | — | Target framework |
- Value Generation Strategy
The annotation
configures SQL Server identity columns globally..HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn)
3. Migration Class Structure
3.1 AddStateTable Migration
This EF Core migration is split into two partial classes:
AddStateTable.cs→ Contains theUp()andDown()methods which add or remove theStatetable.AddStateTable.Designer.cs→ Contains theBuildTargetModel(ModelBuilder)snapshot (this file).
Note: You normally do not edit the
.Designer.csfile manually.
3.2 Designer File
- Namespace:
IrbyImaging.DbContext.Migrations - Class:
partial class AddStateTable - Core Method:
Populatesprotected override void BuildTargetModel(ModelBuilder modelBuilder)modelBuilderwith every entity, property mapping, key, index, schema, and relationship expected after this migration.
4. BuildTargetModel Method
Within BuildTargetModel, EF Core:
- Configures global annotations
modelBuilder .HasAnnotation("Relational:MaxIdentifierLength", 128) .HasAnnotation("ProductVersion", "5.0.12"); - Defines each entity by calling
modelBuilder.Entity(...) - For each entity:
- Registers primary key via
HasKey(...) - Defines properties with types, lengths, nullability
- Adds indexes via
HasIndex(...) - Maps to table & schema:
ToTable("Name", "irby") - Configures inheritance via
HasDiscriminator() - Configures relationships & navigation properties
- Registers primary key via
5. Entities & Data Models
5.1 State Entity 🗺️
This migration introduces the State table.
modelBuilder.Entity("IrbyImaging.DomainClasses.State", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
b.Property<string>("Abbreviation")
.HasColumnType("nvarchar(max)");
b.Property<string>("Code")
.HasColumnType("nvarchar(max)");
b.Property<string>("Name")
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("State", "irby");
});
| Column | CLR Type | SQL Type | Nullable | Notes |
|---|---|---|---|---|
| Id | Guid |
uniqueidentifier |
No | PK, auto-generated |
| Abbreviation | string |
nvarchar(max) |
Yes | e.g. "CA" |
| Code | string |
nvarchar(max) |
Yes | Custom state code (e.g. "06" for CA) |
| Name | string |
nvarchar(max) |
Yes | Full state name (e.g. "California") |
5.2 Other Entities (Summary)
This snapshot includes 30+ domain classes. A few highlights:
| Entity | Table Name | Schema | Key Type |
|---|---|---|---|
| Asset | Asset | irby | Guid (PK) |
| AssetHistory | AssetHistory | irby | Guid |
| AssetModel | AssetModel | irby | int (IDENTITY) |
| BillingInvoice | BillingInvoice | irby | Guid |
| CarrierType & CarrierTypeDetail | CarrierTypeDetail | irby | Guid |
| DocumentImage | DocumentImage | irby | Guid (PK) |
| Employee | Employee | irby | Guid |
| FieldOffice | FieldOffice | irby | Guid |
| … and many more (Job, Ticket, Update, etc.) | … | … | … |
⚠️ For a full list, search for
modelBuilder.Entity("IrbyImaging.DomainClasses...")
6. Relationships & Navigation Properties
After defining entities, the designer wires up:
- One‑to‑Many:
Employee→ manyAssets(owner)FieldOffice→ manyAssetsJob→ manyEmployees
- Inheritance:
Computer,MobileDevice,DeskPhone, etc. inherit fromAsset, using a discriminator
- Foreign Keys
modelBuilder.Entity("IrbyImaging.DomainClasses.Asset") .HasOne("FieldOffice", "FieldOffice") .WithMany("Assets") .HasForeignKey("FieldOfficeId");
Navigation properties are configured via calls to
.HasOne(...).WithMany(...)and.Navigation(...).
7. How to Apply the Migration 🚀
- Add Migration (already done):
dotnet ef migrations add AddStateTable - Review Designer Snapshot
- Located under
Migrations/20220804191712_AddStateTable.Designer.cs
- Located under
- Update Database:
dotnet ef database update - Verify
- Confirm
Statetable exists in theirbyschema. - Check that all other tables and constraints are intact.
- Confirm
8. UML Diagram
classDiagram
class State {
+Guid Id
+string Abbreviation
+string Code
+string Name
}
%% Inheritance Example
class Asset
class Computer
Asset <|-- Computer : Discriminator="Computer"
- State is a standalone entity with no FKs in this migration.
- Asset has subtypes (
Computer,MobileDevice, etc.) using a single table inheritance with discriminator.
9. Sample Code Snippets
Querying States
using var context = new IrbyContext();
List<State> allStates = await context.Set<State>()
.OrderBy(s => s.Name)
.ToListAsync();
Seeding Initial Data (in DbContext)
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<State>().HasData(
new State { Id = Guid.NewGuid(), Name = "California", Code = "06", Abbreviation = "CA" },
new State { Id = Guid.NewGuid(), Name = "New York", Code = "36", Abbreviation = "NY" }
// ... more states
);
}
10. Conclusion 🔚
The AddStateTable.Designer.cs file represents a snapshot of the EF Core model after introducing the State table. It ensures:
- Precise mapping of CLR classes to SQL tables/columns.
- All existing entities, keys, indexes, inheritance, and relationships remain consistent.
- Future migrations can be correctly scaffolded by comparing the new model to this snapshot.
✨ Tip: Always review the auto-generated designer code to catch unintended model changes before applying them to production.
Happy coding! 🚀