π¦ IrbyImaging.DbContext.Migrations
20221103025134_DeleteColumnCustomerTable.Designer.cs Documentation
π Table of Contents
- π― Overview & Purpose
- π οΈ Dependencies & Technologies
- π Key Functional Areas
- π¦ Data Models (Entities) & Mappings
- π§© Relationships & Constraints
- ποΈ Migrations & Versioning
- π Usage Example
- π Conclusion
π― 1. Overview & Purpose
This file is an EF Core Migrations Designerβan autoβgenerated snapshot of your Entity Framework Core model at a given point in time.
- Filename:
20221103025134_DeleteColumnCustomerTable.Designer.cs - Namespace:
IrbyImaging.DbContext.Migrations - Context:
IrbyContext - Migration:
DeleteColumnCustomerTable
Why it exists
When you add or remove properties on your domain classes and rundotnet ef migrations add, EF Core creates two files:
- A migration file (
Up/Downmethods to apply/revert changes)- A designer file (this file), which captures the BuildTargetModel, i.e. the final state of the EF model after applying the migration.
π οΈ 2. Dependencies & Technologies
| Technology | Purpose |
|---|---|
| .NET 5.0 | Runtime & Base Class Library |
| EntityFrameworkCore | ORM for .NET, model building & migrations |
| C# | Language of the generated code |
| SQL Server | Underlying relational database |
Key using statements:
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
π 3. Key Functional Areas
- BuildTargetModel
- Central method that describes all entities, their properties, table mappings, indexes, keys, and relationships.
- Entity Definitions
- For each domain class (
Asset,Customer,Employee, β¦), the code usesmodelBuilder.Entity(...)to set up columns and constraints.
- For each domain class (
- Discriminator Setup
- Supports table-per-hierarchy (TPH) for the
Assetclass hierarchy (Computer,DeskPhone,MobileDevice, β¦).
- Supports table-per-hierarchy (TPH) for the
- Navigation & Relationships
- Defines foreignβkey relationships and navigation properties to enforce referential integrity.
π¦ 4. Data Models (Entities) & Mappings
Below is a sample of how EF Core maps a domain class to database schema. This designer file contains 38+ entities. We highlight a few key ones:
πΉ Customer Entity
| Property | Type | Attributes |
|---|---|---|
| Id | Guid |
PK, ValueGeneratedOnAdd |
| CustomerNumber | string |
nvarchar(max), nullable |
| Name | string |
nvarchar(max), nullable |
modelBuilder.Entity("IrbyImaging.DomainClasses.Customer", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
b.Property<string>("CustomerNumber")
.HasColumnType("nvarchar(max)");
b.Property<string>("Name")
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Customer", "irby");
});
πΉ Asset Entity Hierarchy
| Base Type | Table | Discriminator | Derived Types |
|---|---|---|---|
| Asset | irby.Asset | Discriminator | Computer, DeskPhone, Dock, MobileDevice, Monitor, NetworkEquipment, β¦ |
// Base mapping
modelBuilder.Entity("IrbyImaging.DomainClasses.Asset", b =>
{
b.HasKey("Id");
b.ToTable("Asset", "irby");
b.Property<string>("Discriminator")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasDiscriminator<string>("Discriminator")
.HasValue("Asset");
});
// Sub-class example
modelBuilder.Entity("IrbyImaging.DomainClasses.Computer", b =>
{
b.HasBaseType("IrbyImaging.DomainClasses.Asset");
b.Property<string>("AssetNumber")
.HasColumnName("AssetNumber");
b.HasDiscriminator()
.HasValue("Computer");
});
π§© 5. Relationships & Constraints
π Foreign Key & Navigation Setup
- Asset β AssetModel (Required, Cascade delete)
- Asset β FieldOffice, Asset β Employee (Optional)
- Employee β Job (Required, Cascade delete)
- FieldOffice β Employee (Manager/PM/Superintendent) (Optional)
modelBuilder.Entity("IrbyImaging.DomainClasses.Asset", b =>
{
b.HasOne("IrbyImaging.DomainClasses.AssetModel", "Model")
.WithMany()
.HasForeignKey("ModelId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
// ...
});
π Mermaid ER Diagram (excerpt)
erDiagram
ASSET ||--o{ ASSET_MODEL : Model
ASSET }o--|| FIELDOFFICE : FieldOffice
EMPLOYEE }o--|| JOB : CurrentJob
FIELDOFFICE }o--|| EMPLOYEE : Manager
ποΈ 6. Migrations & Versioning
- Migration Name
DeleteColumnCustomerTable - Designer File Role
- Captures the target state (post-migration) of the EF Core model.
- Does not contain the
Up()/Down()logic (thatβs in the other file:20221103025134_DeleteColumnCustomerTable.cs).
Tip: When you run
dotnet ef migrations add DeleteColumnCustomerTable, EF compares your current model to the last snapshot, generates anUp/Downscript, then updates the snapshot (this file).
π 7. Usage Example
1. Apply the Migration
cd /path/to/IrbyImaging.DbContext
dotnet ef database update DeleteColumnCustomerTable
2. Verify Schema Change
Use SQL Server Management Studio or Azure Data Studio to confirm the Customer table no longer contains the deleted column.
3. Rollback (if needed)
dotnet ef database update [PreviousMigrationName]
π 8. Conclusion
This designer file is a critical part of EF Coreβs migration pipeline:
- π¨ Snapshot of your model at point-in-time.
- π Feeds EF when generating future migrations.
- β οΈ Do not modify manuallyβitβs autoβgenerated.
Understanding this file helps you:
- Troubleshoot model drift.
- Review how EF sees your domain classes.
- Ensure your database schema matches your C# model.
Happy coding! π
Generated by Entity Framework Core 5.0.12