📖 AddEquipmentTable.Designer.cs Documentation


Table of Contents

  1. 🎯 Overview & Purpose
  2. 🛠️ Dependencies & Technologies
  3. 🏗️ BuildTargetModel Method
    • 3.1. Model Annotations
    • 3.2. Entity Registrations
    • 3.3. Relationships & Navigation Properties
  4. 🔍 Focus: Equipment Table
    • 4.1. Schema & Columns
    • 4.2. Indexes & Keys
    • 4.3. Foreign‑Key Relationship
    • 4.4. Sample Usage
  5. ⚙️ Applying the Migration
  6. 💡 UML Diagram (Partial)
  7. ✅ Conclusion

1. 🎯 Overview & Purpose

The file AddEquipmentTable.Designer.cs is an auto‑generated EF Core Migration Designer file. It contains:

  • A snapshot of the EF Core model as it should appear after applying the 20240229185324_AddEquipmentTable migration.
  • The method BuildTargetModel(ModelBuilder) which defines all entities, their properties, keys, indexes, tables & relationships.
  • A record of adding the new Equipment table into the database schema.

Why it exists:
EF Core Migrations generate two files per migration:

  1. <timestamp>_AddEquipmentTable.cs – the operations (e.g., CreateTable).
  2. <timestamp>_AddEquipmentTable.Designer.cs – the snapshot of the entire model after those operations.

The snapshot helps EF Core understand current state vs. future changes.


2. 🛠️ Dependencies & Technologies

Technology Version Usage
Microsoft.EntityFrameworkCore 7.0.7 Core ORM
Microsoft.EntityFrameworkCore.SqlServer 7.0.7 SQL Server provider
Microsoft.EntityFrameworkCore.Migrations 7.0.7 Migration services
.NET 7 Target framework
<!-- Example csproj snippet -->
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.7">
  <PrivateAssets>all</PrivateAssets>
</PackageReference>

3. 🏗️ BuildTargetModel Method

The core of this designer file is:

protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
    #pragma warning disable 612, 618
    modelBuilder
        .HasAnnotation("ProductVersion", "7.0.7")
        .HasAnnotation("Relational:MaxIdentifierLength", 128);

    SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);

    // … hundreds of Entity registrations …

    #pragma warning restore 612, 618
}

3.1. Model Annotations

  • ProductVersion: Tracks EF Core version for compatibility.
  • Relational:MaxIdentifierLength: Maximum length of database identifiers (e.g., 128 for SQL Server).
  • UseIdentityColumns(): Globally configures integer PKs to use SQL Server’s IDENTITY feature.

3.2. Entity Registrations

Each call to modelBuilder.Entity("Namespace.ClassName", b => { … }) configures an entity. For every table, EF Core:

  1. Declares Properties (columns), their CLR type, and column types.
  2. Sets up Keys with .HasKey(...).
  3. Configures Indexes with .HasIndex(...).
  4. Maps to a schema and table via .ToTable("TableName", "Schema").
  5. (If using TPH inheritance) configures .HasDiscriminator().

Example:

modelBuilder.Entity("IrbyImaging.DomainClasses.Asset", b => {
  b.Property<Guid>("Id").ValueGeneratedOnAdd().HasColumnType("uniqueidentifier");
  b.HasKey("Id");
  b.ToTable("Asset", "irby");
  // … discriminator, indexes …
});

3.3. Relationships & Navigation Properties

After properties, EF Core wires up relationships:

modelBuilder.Entity("IrbyImaging.DomainClasses.Equipment", b => {
  b.HasOne("IrbyImaging.DomainClasses.Job", "Job")
   .WithMany()
   .HasForeignKey("JobId")
   .OnDelete(DeleteBehavior.Cascade)
   .IsRequired();
  b.Navigation("Job");
});
  • HasOne/WithMany: Defines a 1‑to‑many relationship.
  • ForeignKey: Points to the FK property (JobId).
  • OnDelete: Cascade deletes equipment when its job is removed.

4. 🔍 Focus: Equipment Table

4.1. Schema & Columns

CLR Type Property Column Type Notes
Guid EquipmentId (PK) uniqueidentifier Identity on Add
Guid JobId (FK) uniqueidentifier Required, cascade delete
string Name nvarchar(max) Equipment name
string SerialNumber nvarchar(max) Manufacturer’s serial
decimal PurchasePrice decimal(18,2)
DateTime? DateAcquired datetime2 Nullable
DateTime? DateDisposed datetime2 Nullable
string Status nvarchar(max) e.g. “Active”, “Disposed”
string UnitTagNumber nvarchar(max) Internal tag number
string … many additional … … See full designer file for details

Note: The file defines every column in the snapshot. Above are the key ones.

4.2. Indexes & Keys

  • Primary Key: .HasKey("EquipmentId")
  • Index on JobId: .HasIndex("JobId")

4.3. Foreign‑Key Relationship

b.HasOne("IrbyImaging.DomainClasses.Job", "Job")
 .WithMany()
 .HasForeignKey("JobId")
 .OnDelete(DeleteBehavior.Cascade)
 .IsRequired();
  • Links Equipment → Job.
  • Cascade delete ensures referential integrity.

4.4. Sample Usage

// Add new equipment to a job
var equipment = new Equipment {
    JobId = existingJobId,
    Name = "Excavator ZX-5",
    SerialNumber = "ZX5-12345",
    PurchasePrice = 150_000m,
    DateAcquired = DateTime.UtcNow,
    Status = "Active"
};
context.Equipments.Add(equipment);
context.SaveChanges();

5. ⚙️ Applying the Migration

  1. Add Migration
    dotnet ef migrations add AddEquipmentTable
    
  2. Update Database
    dotnet ef database update
    
  3. The .Designer.cs file automatically reflects the new Equipment table in the snapshot.

6. 💡 UML Diagram (Partial)

classDiagram
  Job        "1" -- "0..*" Equipment : has
  Equipment  : +Guid EquipmentId
  Equipment  : +Guid JobId
  Equipment  : +string Name
  Equipment  : +string SerialNumber
  Equipment  : +decimal PurchasePrice

This class diagram shows the one-to-many relationship:
A single Job can have zero or more Equipment records.


7. ✅ Conclusion

  • AddEquipmentTable.Designer.cs is the EF Core model snapshot capturing the state after the Equipment table is introduced.
  • It enumerates all entities, but our focus is on how it wires up the new Equipment entity:
    • 👑 Primary Key
    • 🔎 Index on JobId
    • 🔗 One-to-Many FK relationship to Job
  • Use dotnet ef database update to apply and enable CRUD operations for Equipment in your application.

Happy coding! 🚀