📓 AddTableInfoForCostCode.Designer.cs Documentation

Path: IrbyImaging.DbContext/Migrations/20250113133014_AddTableInfoForCostCode.Designer.cs
Generated by: Entity Framework Core Migrations
Purpose: Provides a “snapshot” of the EF Core model at the point when the AddTableInfoForCostCode migration was added.


📑 Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Key Functional Areas
  4. How to Use This Migration
  5. Code Snippets & Examples
  6. Visual UML Diagram
  7. Conclusion

🌟 Overview & Purpose

This auto-generated file is part of the Entity Framework Core Migrations infrastructure. It represents the target model—that is, the complete EF Core schema—right after applying the migration named AddTableInfoForCostCode.

  • Why it matters:
    • EF Core uses this model snapshot to compare future changes and generate incremental migrations.
    • Ensures your database schema stays in sync with your domain classes.

🔗 Dependencies & Technologies

Technology Version / Package
.NET 7.0+ (C# 11)
Entity Framework Core 9.0.0 (Microsoft.EntityFrameworkCore)
EF Core SQL Server Provider Microsoft.EntityFrameworkCore.SqlServer
Data Annotations & Fluent API Microsoft.EntityFrameworkCore.Metadata

🔑 Key Functional Areas

1. BuildTargetModel Method

protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
    // 1️⃣ Add product version and SQL Server identity configuration
    modelBuilder
        .HasAnnotation("ProductVersion", "9.0.0")
        .HasAnnotation("Relational:MaxIdentifierLength", 128);

    SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);

    // 2️⃣ Define each entity… (truncated for brevity)
    modelBuilder.Entity("IrbyImaging.DomainClasses.CostCode", b => { … });
    // …hundreds of entities…
}
  • Responsibilities:
    • Configures global annotations (e.g., EF version, identifier length).
    • Registers each entity type with properties, keys, indexes, and table mappings.
    • Applies inheritance mappings (TPH) for asset hierarchies.
    • Establishes foreign‐key relationships and navigation properties.

2. Entity Definitions

  • Syntax Highlights:
    • b.Property<Guid>("CostCodeId").ValueGeneratedOnAdd()… → declares a primary‐key property.
    • b.ToTable("CostCode", "irby") → maps the CLR type to a database table in the irby schema.
    • .HasIndex(...).IsUnique() → configures database indexes.
    • b.HasDiscriminator().HasValue("MobileDevice") → defines inheritance discriminator for TPH.

3. CostCode Table Addition

This migration introduces the CostCode entity into the model. Its definition in the snapshot:

CLR Type Table Schema
IrbyImaging.DomainClasses.CostCode CostCode irby

Columns & Configuration:

Property Type Constraints
CostCodeId uniqueidentifier Primary Key, auto‐generated
CostCodeNumber int Required
Name nvarchar(max) Required
modelBuilder.Entity("IrbyImaging.DomainClasses.CostCode", b =>
{
  b.Property<Guid>("CostCodeId")
   .ValueGeneratedOnAdd()
   .HasColumnType("uniqueidentifier");

  b.Property<int>("CostCodeNumber")
   .HasColumnType("int");

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

  b.HasKey("CostCodeId");
  b.ToTable("CostCode", "irby");
});

4. Relationships & Navigation

Although CostCode stands alone, the snapshot defines hundreds of relationships—for example:

  • Assets ⇄ FieldOffice
  • Employee ⇄ Department, Role, Job
  • WorkOrder ⇄ Job, Employee
  • TimeSheetEntry ⇄ Employee, TimeEntryType

Tip: To explore relationships, search HasOne( or .WithMany( within BuildTargetModel.


🚀 How to Use This Migration

  1. Add Migration:
    dotnet ef migrations add AddTableInfoForCostCode
    
  2. Apply to Database:
    dotnet ef database update
    
  3. Verify Schema:
    • Confirm the new irby.CostCode table exists with the correct columns.
    • Use SQL Server Management Studio or SELECT * FROM irby.CostCode.

📝 Code Snippets & Examples

Up / Down Methods (in the non‐Designer file)

public partial class AddTableInfoForCostCode : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.CreateTable(
            name: "CostCode",
            schema: "irby",
            columns: table => new
            {
                CostCodeId = table.Column<Guid>(nullable: false),
                CostCodeNumber = table.Column<int>(nullable: false),
                Name = table.Column<string>(nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_CostCode", x => x.CostCodeId);
            });
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropTable(name: "CostCode", schema: "irby");
    }
}

Querying the New Table

using(var context = new IrbyContext())
{
    var codes = await context.Set<CostCode>()
                             .Where(c => c.CostCodeNumber > 100)
                             .OrderBy(c => c.Name)
                             .ToListAsync();
    Console.WriteLine("Retrieved {0} cost codes.", codes.Count);
}

📊 Visual UML Diagram

classDiagram
    class CostCode {
      <<Entity>> 
      +Guid CostCodeId
      +int CostCodeNumber
      +string Name
    }

Note: The full model includes dozens of entities; above is a focused view of the new addition.


🏁 Conclusion

  • What you have learned:

    • The Designer file captures the complete model snapshot for EF Core Migrations.
    • The AddTableInfoForCostCode migration adds/configures the irby.CostCode table.
    • EF Core uses this snapshot to diff future changes and generate further migrations.
  • Next Steps:

    • Extend your domain model with additional entities or properties.
    • Run dotnet ef migrations add <Name> to capture changes.
    • Always verify the generated SQL and resulting database schema.

Happy coding! 🚀