š 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
- Overview & Purpose
- Context & Dependencies
- EF Core Migration Mechanism
- AddNotesColumnToTimeEntry Migration
- BuildTargetModel Method
- Model Snapshot Highlights
- TimeSheetEntry Changes
- Mermaid UML Diagram
- Sample Usage
- 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
IrbyContextDbContext. - A
BuildTargetModelmethod 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
- Scaffold:
Add-Migration AddNotesColumnToTimeEntry - Designer Generated: Creates two files:
*_AddNotesColumnToTimeEntry.cs(Up/Down methods)*_AddNotesColumnToTimeEntry.Designer.cs(Model snapshot)
- Apply:
Update-Databasereads 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
Notescolumn (nvarchar(max)) to the existingTimeSheetEntrytable.
5. BuildTargetModel Method
The heart of the designer file. It:
- Annotates the model with provider-specific settings.
- Registers every entity in the model (
Asset,Employee,Job, ...). - Configures properties, keys, indexes, relationships.
- 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
- Type:
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
- Scaffold Migration
Add-Migration AddNotesColumnToTimeEntry - Inspect
_AddNotesColumnToTimeEntry.cs: Up/Down methods_AddNotesColumnToTimeEntry.Designer.cs: Model snapshot
- Apply to Database
Update-Database - 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
Notescolumn toTimeSheetEntry. - 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!