šŸ“„ IrbyImaging.DbContext/Migrations/20230707061245_AddNotesColumnToTimeEntry.Designer.cs

A detailed look at the auto‑generated EF Core migration designer that adds a Notes column to the TimeSheetEntry table. This document explains its purpose, structure, and how to use it in your application.


Table of Contents

  1. Overview & Purpose
  2. Context & Dependencies
  3. EF Core Migration Mechanism
  4. AddNotesColumnToTimeEntry Migration
  5. BuildTargetModel Method
  6. Model Snapshot Highlights
  7. TimeSheetEntry Changes
  8. Mermaid UML Diagram
  9. Sample Usage
  10. Conclusion

1. Overview & Purpose

šŸ› ļø What is this file?
This file is an EF Core Migration Designer (.Designer.cs) for the migration named 20230707061245_AddNotesColumnToTimeEntry. It contains:

  • The metadata attributes linking it to the IrbyContext DbContext.
  • A BuildTargetModel method that defines a snapshot of the entire model after applying this migration.

šŸ’” Why is it important?

  • Enables Entity Framework Core to track schema changes over time.
  • Used by EF Core tools (Add-Migration, Update-Database) to compute diffs.
  • Should not be manually edited—regenerated on each migration scaffold.

2. Context & Dependencies

Technology Version / Package
.NET Runtime .NET 7.0
Entity Framework Core Microsoft.EntityFrameworkCore 7.0.7
EF Core Tools Microsoft.EntityFrameworkCore.Design
Database Provider Microsoft.EntityFrameworkCore.SqlServer
Nullable Reference Types Disabled (#nullable disable)

Namespace:

namespace IrbyImaging.DbContext.Migrations

Target DbContext:

[DbContext(typeof(IrbyContext))]

3. EF Core Migration Mechanism

  1. Scaffold: Add-Migration AddNotesColumnToTimeEntry
  2. Designer Generated: Creates two files:
    • *_AddNotesColumnToTimeEntry.cs (Up/Down methods)
    • *_AddNotesColumnToTimeEntry.Designer.cs (Model snapshot)
  3. Apply: Update-Database reads metadata, diffs models, executes SQL.

šŸ”— See also: EF Core Migrations Docs


4. AddNotesColumnToTimeEntry Migration

[Migration("20230707061245_AddNotesColumnToTimeEntry")]
partial class AddNotesColumnToTimeEntry
{
    /// <inheritdoc />
    protected override void BuildTargetModel(ModelBuilder modelBuilder)
    {
        // … entire model definitions …
    }
}
  • Migration ID: 20230707061245_AddNotesColumnToTimeEntry
  • Purpose: Add a new Notes column (nvarchar(max)) to the existing TimeSheetEntry table.

5. BuildTargetModel Method

The heart of the designer file. It:

  1. Annotates the model with provider-specific settings.
  2. Registers every entity in the model (Asset, Employee, Job, ...).
  3. Configures properties, keys, indexes, relationships.
  4. Reflects the schema after applying the migration.
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
    #pragma warning disable 612, 618
    modelBuilder
        .HasAnnotation("ProductVersion", "7.0.7")
        .HasAnnotation("Relational:MaxIdentifierLength", 128);

    SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);

    // Entity registrations...
    modelBuilder.Entity("IrbyImaging.DomainClasses.TimeSheetEntry", b =>
    {
        b.Property<Guid>("Id").HasColumnType("uniqueidentifier");
        // … other properties …
        b.Property<string>("Notes")   // ← NEW COLUMN
         .HasColumnType("nvarchar(max)");
        b.ToTable("TimeSheetEntry", "irby");
    });

    // … many other entities …
    #pragma warning restore 612, 618
}

6. Model Snapshot Highlights

Below is a condensed table of some key entities showing their purpose:

Entity Table (Schema) Key Notes
TimeSheetEntry irby.TimeSheetEntry Id (Guid) Added: Notes (nvarchar(max))
TimeEntryType irby.TimeEntryType Id (Guid)
Employee irby.Employee Id (Guid) JDE number unique index
Job irby.Job Id (Guid) JDE number unique index
Asset / Computer / MobileDevice irby.Asset Id (Guid) TPH inheritance with discriminator
… … … …

7. TimeSheetEntry Changes

šŸŽÆ Focus: the new Notes column.

modelBuilder.Entity("IrbyImaging.DomainClasses.TimeSheetEntry", b =>
{
    b.Property<Guid>("Id")
     .ValueGeneratedOnAdd()
     .HasColumnType("uniqueidentifier");

    b.Property<DateTime>("DateWorked")
     .HasColumnType("Date");

    b.Property<decimal>("HoursWorked")
     .HasColumnType("decimal(18,2)");

    b.Property<bool>("IsDeleted")
     .HasColumnType("bit");

    b.Property<string>("Notes")                // <-- Added in this migration
     .HasColumnType("nvarchar(max)");

    b.Property<Guid?>("EmployeeId")
     .HasColumnType("uniqueidentifier");

    b.Property<Guid?>("TimeEntryTypeId")
     .HasColumnType("uniqueidentifier");

    b.ToTable("TimeSheetEntry", "irby");
});
  • Notes:
    • Type: nvarchar(max)
    • Nullable by default
    • Allows free‑form text for time‑entry comments

8. Mermaid UML Diagram

classDiagram
    class TimeSheetEntry {
        +Guid Id
        +DateTime DateWorked
        +decimal HoursWorked
        +bool IsDeleted
        +string Notes
        +Guid? EmployeeId
        +Guid? TimeEntryTypeId
    }
    class Employee {
        +Guid Id
        +string DisplayName
        +string JdeNumber
        +Guid CurrentJobId
    }
    class TimeEntryType {
        +Guid Id
        +string Name
    }
    TimeSheetEntry --> Employee : EmployeeId
    TimeSheetEntry --> TimeEntryType : TimeEntryTypeId
    Employee --> Job : CurrentJobId

9. Sample Usage

  1. Scaffold Migration
    Add-Migration AddNotesColumnToTimeEntry
    
  2. Inspect
    • _AddNotesColumnToTimeEntry.cs: Up/Down methods
    • _AddNotesColumnToTimeEntry.Designer.cs: Model snapshot
  3. Apply to Database
    Update-Database
    
  4. Querying
    using var context = new IrbyContext();
    var entry = new TimeSheetEntry {
        DateWorked = DateTime.Today,
        HoursWorked = 8m,
        Notes = "Reviewed project requirements"
    };
    context.TimeSheetEntries.Add(entry);
    context.SaveChanges();
    

10. Conclusion

āœ… Key Takeaways

  • This designer file captures the entire EF Core model after adding the Notes column to TimeSheetEntry.
  • It is auto‑generated and should remain untouched.
  • When running migrations, EF Core uses it to calculate what SQL DDL to emit.

šŸŽ‰ With this migration in place, your application can store and retrieve rich notes alongside each time entry—boosting productivity and traceability!