📚 Migration Documentation: AddWorkEmailToEmployeeTable.Designer.cs


File Path:
IrbyImaging.DbContext/Migrations/20230717144622_AddWorkEmailToEmployeeTable.Designer.cs


Table of Contents

  1. Overview & Purpose
  2. Migration Class Structure
  3. BuildTargetModel Method
  4. Changes Introduced by This Migration
  5. Key Entities & Data Models
  6. How to Apply the Migration
  7. EF Core Migrations Flow
  8. UML Diagram: Employee Entity
  9. Sample Usage & Code Snippets
  10. Conclusion & Best Practices

1. Overview & Purpose 🎯

This file is an auto-generated snapshot of your Entity Framework Core (EF Core) model after applying the AddWorkEmailToEmployeeTable migration.
It captures:

  • The schema of all entities in your IrbyContext at this point in time.
  • Relationships, indexes, data types, and the new WorkEmail property on Employee.

Note: You should not manually edit this file. It's used by EF Core to perform rollbacks and model comparisons.


2. Migration Class Structure ⚙️

[DbContext(typeof(IrbyContext))]
[Migration("20230717144622_AddWorkEmailToEmployeeTable")]
partial class AddWorkEmailToEmployeeTable : Migration
{
    protected override void BuildTargetModel(ModelBuilder modelBuilder)
    {
        // EF Core model snapshot
    }
}
  • Attributes

    • [DbContext(typeof(IrbyContext))]: Binds migration to your IrbyContext.
    • [Migration("20230717144622_AddWorkEmailToEmployeeTable")]: Unique timestamp & name.
  • Method

    • BuildTargetModel(ModelBuilder modelBuilder): Reconstructs the entire model after this migration runs.

3. BuildTargetModel Method 🛠️

The BuildTargetModel method uses the fluent API to define:

  • Schemas & Tables
  • Columns (types, nullability)
  • Keys, Indexes
  • Relationships (FKs, cascade rules)
  • Inheritance (TPH – Table-per-Hierarchy)

The snapshot is complete—every entity and property your context knows about.


4. Changes Introduced by This Migration ✨

The primary purpose of 20230717144622_AddWorkEmailToEmployeeTable is to add:

  • WorkEmail property on the Employee entity.

Before Migration

Column Type Nullable?
EmployeeId uniqueidentifier No
Email nvarchar(max) Yes
… … …

After Migration

Column Type Nullable?
EmployeeId uniqueidentifier No
Email nvarchar(max) Yes
WorkEmail nvarchar(max) Yes
… … …

EF Core uses modelBuilder.Entity("IrbyImaging.DomainClasses.Employee", b => { … })
to define each property. You’ll see the new line:

b.Property<string>("WorkEmail")
  .HasColumnType("nvarchar(max)");

5. Key Entities & Data Models 📋

While the snapshot includes 40+ entities (e.g., Asset, Job, TimeSheetEntry), here is a concise look at the Employee model:

Property CLR Type Column Type Notes
EmployeeId Guid uniqueidentifier Primary Key
FirstName string nvarchar(max)
LastName string nvarchar(max)
Email string nvarchar(max)
WorkEmail (new) string nvarchar(max) Added by this migration
DepartmentId Guid? uniqueidentifier FK → Department
RoleId Guid? uniqueidentifier FK → Role
JobId Guid uniqueidentifier FK → Job (Required)
JdeNumber string nvarchar(450) Unique Index
EmploymentStartDate DateTime? datetime2
TerminatedDate DateTime? datetime2
… … …

6. How to Apply the Migration 🚀

  1. Add the property in your domain model:
    public class Employee
    {
        public Guid EmployeeId { get; set; }
        public string Email { get; set; }
        public string WorkEmail { get; set; }   // ← New
        // … other properties
    }
    
  2. Scaffold a new migration:
    dotnet ef migrations add AddWorkEmailToEmployeeTable
    
  3. Apply migration to your database:
    dotnet ef database update
    

After update, the Employee table in SQL Server will include the new WorkEmail column.


7. EF Core Migrations Flow 🔄

flowchart LR
   A[Model Change in Code] --> B[Add Migration]
   B --> C[Designer & Up/Down Files]
   C --> D[Update Database]
   D --> E[Schema Updated]
   E --> F[Model Snapshot Updated]
  • Designer.cs: Contains the snapshot (this file).
  • Up() / Down(): Actual SQL operations to add/drop the WorkEmail column.

8. UML Diagram: Employee Entity 📐

classDiagram
    IrbyImaging.DomainClasses.Employee <|-- Employee
    class Employee {
        +Guid EmployeeId
        +string FirstName
        +string LastName
        +string Email
        +string WorkEmail
        +Guid? DepartmentId
        +Guid? RoleId
        +Guid JobId
        +string JdeNumber
        +DateTime? EmploymentStartDate
        +DateTime? TerminatedDate
    }
    Employee --> Department : FK
    Employee --> Role : FK
    Employee --> Job  : FK

9. Sample Usage & Code Snippets 🖥️

Setting WorkEmail in Application Code

using(var context = new IrbyContext())
{
    // Fetch an employee
    var emp = context.Employees.First(e => e.JdeNumber == "12345");
    emp.WorkEmail = "[email protected]";

    context.SaveChanges();  // Persists to SQL DB
}

Querying by WorkEmail

var user = context.Employees
    .Where(e => e.WorkEmail.EndsWith("@irby.com"))
    .ToList();

Rolling Back (Down Migration)

dotnet ef database update 20230717144621_PreviousMigration

This will remove the WorkEmail column.


10. Conclusion & Best Practices 🏁

  • Keep it Clean: Let EF Core generate your designer snapshot—do not edit manually.
  • Version Control: Commit both the .Designer.cs and the corresponding Up/Down files.
  • Review Changes: Use tools like SQL Profiler or Visual Studio’s SQL Compare to validate schema updates.
  • Consistent Naming: Follow a clear naming convention (AddXxxToYyy) to keep migrations organized.

✨ With this migration, your domain model and database stay in sync, enabling you to reliably use the new WorkEmail field across your application! 🚀