📚 Documentation: AddApprovalDateMechanic.Designer.cs


🗂️ Table of Contents

  1. Introduction & Context
  2. EF Core Migrations Overview
  3. File: AddApprovalDateMechanic.Designer.cs
  4. How to Use This Migration
  5. Sample Code Snippets
  6. UML Diagram
  7. Conclusion 🎉

🎯 Introduction & Context

The AddApprovalDateMechanic.Designer.cs file is an auto‑generated EF Core migration snapshot. It lives under:

IrbyImaging.DbContext/Migrations/20250303160308_AddApprovalDateMechanic.Designer.cs

Its main job is to record the state of your entity model after applying the AddApprovalDateMechanic migration. EF Core uses this “model snapshot” when generating future migrations to detect changes.


🔧 EF Core Migrations Overview

  • Up/Down Migrations

    • Up(...): Applies schema changes (e.g., adds a column).
    • Down(...): Reverts those changes.
  • Designer File

    • BuildTargetModel(ModelBuilder)
      • Captures the complete model configuration (tables, columns, relationships) after the migration.
      • Used by EF Core to compare current model vs. next migration.
  • Migration Process

    1. dotnet ef migrations add AddApprovalDateMechanic
    2. EF Core scaffolds *.cs and *.Designer.cs.
    3. dotnet ef database update applies the Up method.

📄 File: AddApprovalDateMechanic.Designer.cs

📝 Purpose & Role

Item Description
Class AddApprovalDateMechanic (partial)
Attributes [DbContext(typeof(IrbyContext))][Migration("20250303160308_AddApprovalDateMechanic")]
Key Method protected override void BuildTargetModel(ModelBuilder modelBuilder)
Responsibility Store the complete EF Core model state after adding the ApprovalDate column to the MechanicTimesheet table.

⚙️ Dependencies & Technologies

  • .NET 9.0.0 (per HasAnnotation("ProductVersion","9.0.0"))
  • Microsoft.EntityFrameworkCore
    • Microsoft.EntityFrameworkCore.Migrations
    • Microsoft.EntityFrameworkCore.Metadata
    • Microsoft.EntityFrameworkCore.Infrastructure
  • SQL Server (identity columns via UseIdentityColumns)

🚀 Key Functional Areas

  1. Model Snapshot
    • Re‑creates every entity mapping: tables, columns, keys, indices, relationships.
  2. Schema Annotations
    • Database provider specifics (UseIdentityColumns, Relational:MaxIdentifierLength).
  3. Discriminator Mapping
    • Table‑per‑hierarchy for inherited types (e.g., Asset subtypes: Computer, MobileDevice, etc.).
  4. Navigation Relationships
    • Defines HasOne, WithMany, OnDelete behaviors.

🔍 BuildTargetModel Method Explained

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

    SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);

    // Example: MechanicTimesheet entity mapping
    modelBuilder.Entity("IrbyImaging.DomainClasses.MechanicTimesheet", b =>
    {
        b.Property<Guid>("Id")
         .ValueGeneratedOnAdd()
         .HasColumnType("uniqueidentifier");

        // NEW COLUMN added by migration:
        b.Property<DateTime>("ApprovedDate")
         .HasColumnType("datetime2");

        // ... other properties, keys, and table name ...
        b.HasKey("Id");
        b.ToTable("MechanicTimesheet","irby");
    });
    // ... ALL other entities ...
    #pragma warning restore 612, 618
}
  • Annotations: Declare EF Core version & identifier length.
  • UseIdentityColumns: Enables SQL Server identity (auto‑increment) for integer PKs.
  • Entity Blocks: For each CLR type:
    • b.Property<...>(...): Defines column type, nullability, defaults.
    • b.HasKey(...): Sets the primary key.
    • b.ToTable(...): Maps CLR type → schema.table.
    • b.HasIndex(...): Defines DB indices.
    • b.HasOne(...)…WithMany(...): Configures relationships.

🗃️ Snapshot of Data Models

This migration supports dozens of entities. Below is a sample list of a few tables captured:

Entity Class Table (Schema) Primary Key
MechanicTimesheet irby.MechanicTimesheet Id (GUID)
FieldTimesheet irby.FieldTimesheet TimesheetId (GUID)
Employee irby.Employee EmployeeId (GUID)
Job irby.Job JobId (GUID)
Asset + TPH Subclasses irby.Asset Id (GUID), Discriminator
… many more (Equipment, Invoice, WIP, etc.)

⚙️ How to Use This Migration

  1. Add Migration
    dotnet ef migrations add AddApprovalDateMechanic
    
  2. Review Generated Files
    • 20250303160308_AddApprovalDateMechanic.cs
    • 20250303160308_AddApprovalDateMechanic.Designer.cs
  3. Apply to Database
    dotnet ef database update
    
  4. Rollback (if needed)
    dotnet ef database update [PreviousMigrationName]
    

💻 Sample Code Snippets

Up/Down Migration (non‑designer file)

public partial class AddApprovalDateMechanic : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.AddColumn<DateTime>(
            name: "ApprovedDate",
            schema: "irby",
            table: "MechanicTimesheet",
            type: "datetime2",
            nullable: false,
            defaultValue: DateTime.UtcNow // or a specific default
        );
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropColumn(
            name: "ApprovedDate",
            schema: "irby",
            table: "MechanicTimesheet");
    }
}

Applying the Migration

# Adds & applies the migration in one go:
dotnet ef migrations add AddApprovalDateMechanic
dotnet ef database update

📊 UML Diagram

classDiagram
    class AddApprovalDateMechanic {
      <<Migration>>
      +Up(migrationBuilder)
      +Down(migrationBuilder)
      -BuildTargetModel(modelBuilder)
    }

    class MechanicTimesheet {
      Id: Guid
      ApprovedDate: DateTime
      // … other columns …
    }

    AddApprovalDateMechanic ..> MechanicTimesheet : adds ➞ ApprovedDate
    MechanicTimesheet --|> Table : "irby.MechanicTimesheet"

🎉 Conclusion

The AddApprovalDateMechanic.Designer.cs file is a snapshot of your EF Core model after the AddApprovalDateMechanic migration. It ensures EF Core can correctly calculate future migrations by comparing the current model with a fresh build of your C# entity classes.

  • No manual edits required – it’s auto‑generated.
  • Keep it under source control for consistent migrations across environments.
  • Use the Up/Down methods in the corresponding .cs file to manage your database schema.

With this setup, adding new properties to domain classes (like ApprovedDate) becomes a breeze—EF Core takes care of the SQL DDL for you! 🚀


Generated with 💙 by EF Core tooling.