📄 Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Migration Class Structure
    3.1 AddStateTable Migration
    3.2 Designer File
  4. BuildTargetModel Method
  5. Entities & Data Models
    5.1 State Entity 🗺️
    5.2 Other Entities (Summary)
  6. Relationships & Navigation Properties
  7. How to Apply the Migration 🚀
  8. UML Diagram
  9. Sample Code Snippets
  10. 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 State table in the irby schema.
  • 🤝 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
    .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn)
    
    configures SQL Server identity columns globally.

3. Migration Class Structure

3.1 AddStateTable Migration

This EF Core migration is split into two partial classes:

  • AddStateTable.cs → Contains the Up() and Down() methods which add or remove the State table.
  • AddStateTable.Designer.cs → Contains the BuildTargetModel(ModelBuilder) snapshot (this file).

Note: You normally do not edit the .Designer.cs file manually.

3.2 Designer File

  • Namespace: IrbyImaging.DbContext.Migrations
  • Class: partial class AddStateTable
  • Core Method:
    protected override void BuildTargetModel(ModelBuilder modelBuilder)
    
    Populates modelBuilder with every entity, property mapping, key, index, schema, and relationship expected after this migration.

4. BuildTargetModel Method

Within BuildTargetModel, EF Core:

  1. Configures global annotations
    modelBuilder
      .HasAnnotation("Relational:MaxIdentifierLength", 128)
      .HasAnnotation("ProductVersion", "5.0.12");
    
  2. Defines each entity by calling modelBuilder.Entity(...)
  3. 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

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 → many Assets (owner)
    • FieldOffice → many Assets
    • Job → many Employees
  • Inheritance:
    • Computer, MobileDevice, DeskPhone, etc. inherit from Asset, 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 🚀

  1. Add Migration (already done):
    dotnet ef migrations add AddStateTable
    
  2. Review Designer Snapshot
    • Located under Migrations/20220804191712_AddStateTable.Designer.cs
  3. Update Database:
    dotnet ef database update
    
  4. Verify
    • Confirm State table exists in the irby schema.
    • Check that all other tables and constraints are intact.

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! 🚀