๐Ÿ“– Documentation: 20250306035117_AddCrewEmployeeTable.Designer.cs


๐Ÿ—‚ Table of Contents

  1. ๐Ÿ™‹โ€โ™‚๏ธ Overview & Purpose
  2. โš™๏ธ Dependencies & Technologies
  3. ๐Ÿ—๏ธ Key Functional Areas
  4. โž• Changes Introduced: CrewEmployee Table
  5. ๐Ÿ” Entity Mapping in BuildTargetModel
  6. ๐Ÿ“Š CrewEmployee Table Definition
  7. ๐Ÿงฉ Relationships & Navigation Properties
  8. ๐Ÿ”ง Usage Examples & Code Snippets
  9. ๐Ÿ“ˆ UML Diagram
  10. ๐Ÿ›  How to Run the Migration
  11. ๐Ÿ“ Conclusion

๐Ÿ™‹โ€โ™‚๏ธ Overview & Purpose

This file is an autoโ€‘generated Entity Framework Core migration snapshot.

  • Location: IrbyImaging.DbContext/Migrations/20250306035117_AddCrewEmployeeTable.Designer.cs
  • Class: AddCrewEmployeeTable (partial)
  • Method: BuildTargetModel(ModelBuilder modelBuilder)

Purpose

  • Captures the state of the EF Core model at the time of migration 20250306035117.
  • Defines table schemas, columns, keys, indexes, and relationships for all domain entitiesโ€”with a special focus on the newly introduced CrewEmployee join table.

โš™๏ธ Dependencies & Technologies

Technology Version / Notes
.NET / C# .NET 7+, C# 10+
Entity Framework Core EF Core 9.0.0 (SQL Server Proยญvider)
SQL Server Relational database with โ€œirbyโ€ schema
Tools dotnet ef migrations add / update-database

๐Ÿ—๏ธ Key Functional Areas

  • Model Annotations

    • modelBuilder.HasAnnotation("ProductVersion", "9.0.0")
    • UseIdentityColumns()
  • Entity Definitions

    • Tables like AccountBalances, Asset, Employee, ... plus the new CrewEmployee.
  • Relationships

    • Oneโ€‘toโ€‘many, manyโ€‘toโ€‘many join tables, inheritance (TPH), indexes, constraints.
  • Schema

    • Default irby schema applied to all tables except some view models and ODS tables.

โž• Changes Introduced: CrewEmployee Table

๐Ÿ” Migration Name: AddCrewEmployeeTable
๐ŸŽฏ Goal: Introduce a manyโ€‘toโ€‘many mapping between Crew and Employee.

  • Each Crew can have multiple Employees.
  • Each Employee can belong to multiple Crews.

๐Ÿ” Entity Mapping in BuildTargetModel

The BuildTargetModel(ModelBuilder modelBuilder) method uses a fluent API to recreate the entire EF Core model.

protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
    modelBuilder
        .HasAnnotation("ProductVersion", "9.0.0")
        .HasAnnotation("Relational:MaxIdentifierLength", 128);

    SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);

    // ... dozens of Entity() configurations ...

    // -->>> Our new mapping appears here
    modelBuilder.Entity("IrbyImaging.DomainClasses.CrewEmployee", b =>
    {
        b.Property<Guid>("CrewId")
            .HasColumnType("uniqueidentifier");
        b.Property<Guid>("EmployeeId")
            .HasColumnType("uniqueidentifier");

        b.HasKey("CrewId", "EmployeeId");
        b.HasIndex("EmployeeId");

        b.ToTable("CrewEmployee", "irby");
    });

    // ... relationship wiring at the end ...
}

๐Ÿ“Š CrewEmployee Table Definition

Column CLR Type SQL Type Constraint
CrewId Guid uniqueidentifier Part of Primary Key, FK โ†’ Crew.CrewId
EmployeeId Guid uniqueidentifier Part of Primary Key, FK โ†’ Employee.EmployeeId
  • Composite Primary Key: (CrewId, EmployeeId)
  • Index: on EmployeeId for faster lookups.
  • Schema: irby
  • Table: CrewEmployee

๐Ÿงฉ Relationships & Navigation Properties

At the bottom of BuildTargetModel, the foreign keys are wired:

modelBuilder.Entity("IrbyImaging.DomainClasses.CrewEmployee", b =>
{
    b.HasOne("IrbyImaging.DomainClasses.Crew", "Crew")
     .WithMany("CrewEmployees")
     .HasForeignKey("CrewId")
     .OnDelete(DeleteBehavior.Restrict)
     .IsRequired();

    b.HasOne("IrbyImaging.DomainClasses.Employee", "Employee")
     .WithMany("CrewEmployees")
     .HasForeignKey("EmployeeId")
     .OnDelete(DeleteBehavior.Restrict)
     .IsRequired();
});
  • Crew โ†’ navigation Crew.CrewEmployees (collection of join records)
  • Employee โ†’ navigation Employee.CrewEmployees

๐Ÿ”ง Usage Examples & Code Snippets

using var context = new IrbyContext();

// Suppose existing crewId and employeeId
var link = new CrewEmployee {
    CrewId     = crewId,
    EmployeeId = employeeId
};

context.Set<CrewEmployee>().Add(link);
context.SaveChanges();

2. Querying All Employees in a Crew

var crewWithEmployees = context.Crews
    .Include(c => c.CrewEmployees)
        .ThenInclude(ce => ce.Employee)
    .FirstOrDefault(c => c.CrewId == crewId);

foreach (var ce in crewWithEmployees.CrewEmployees)
{
    Console.WriteLine(ce.Employee.DisplayName);
}

3. Removing an Employee from a Crew

var link = context.Set<CrewEmployee>()
    .SingleOrDefault(ce => ce.CrewId == crewId && ce.EmployeeId == employeeId);

if (link != null) {
    context.Remove(link);
    context.SaveChanges();
}

๐Ÿ“ˆ UML Diagram

classDiagram
    CrewEmployee "1" -- "0..*" Crew : CrewId
    CrewEmployee "1" -- "0..*" Employee : EmployeeId
    Crew "1" -- "0..*" CrewEmployee : has
    Employee "1" -- "0..*" CrewEmployee : belongs to

๐Ÿ›  How to Run the Migration

  1. Add the migration (already done):
    dotnet ef migrations add AddCrewEmployeeTable
    
  2. Apply the migration:
    dotnet ef database update
    
  3. Verify: Check irby.CrewEmployee in your SQL Server Management Studio.

๐Ÿ“ Conclusion

  • This Designer file is the snapshot of your EF Core model at migration 20250306035117.
  • It fully describes all entities and relationshipsโ€”particularly the new CrewEmployee manyโ€‘toโ€‘many join.
  • No manual edits are required; EF Core regenerates it during migration scaffolding.

Feel free to reference this document when inspecting the database schema, troubleshooting migrations, or writing repository code around the Crew โ†” Employee relationship. ๐Ÿ‘