πŸ“„ IrbyImaging.DbContext/Migrations/20220303152541_AddFileName.Designer.cs Documentation


πŸ“‘ Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Key Functional Areas
  4. BuildTargetModel Method
  5. Entity Configurations
  6. Entity Relationships & Inheritance
  7. Applying Migrations & Usage Examples
  8. UML Diagrams
  9. Conclusion

1. Overview & Purpose πŸ› οΈ

This file, 20220303152541_AddFileName.Designer.cs, is an auto-generated portion of an Entity Framework Core (EF Core) migration named AddFileName. It lives in the IrbyImaging.DbContext.Migrations namespace and contains the model snapshot for your database schema after applying this migration.

  • Filename: Migrations/20220303152541_AddFileName.Designer.cs
  • Namespace: IrbyImaging.DbContext.Migrations
  • EF Core Version: 5.0.12
  • Generated On: 03‑31‑2022
  • Purpose:
    • Provide a snapshot of the EF Core model (tables, columns, keys, relationships)
    • Used by EF Core tooling to diff schema changes when creating future migrations

2. Dependencies & Technologies πŸ”—

Technology Version/Detail
.NET Runtime .NET 5.0+
Entity Framework Core 5.0.12
EF Core Migrations Migration base class, CLI & Design-time APIs
Database Provider Microsoft SQL Server (SqlServerValueGenerationStrategy.IdentityColumn)

Namespaces Imported

using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

3. Key Functional Areas πŸ“Œ

  1. Model Snapshot Configuration
    • BuildTargetModel: Central method to describe the entire EF model.
  2. Entity Type Registrations
    • Defines each domain class (Asset, Employee, DocumentImage, etc.).
  3. Table & Schema Mappings
    • Specifies table names (e.g., "Asset") and schema (e.g., "irby").
  4. Columns & Data Types
    • Declares properties, their CLR types, database types (nvarchar, uniqueidentifier, etc.).
  5. Keys & Indexes
    • Primary keys (HasKey), alternate keys, foreign keys, indexes (HasIndex).
  6. Inheritance & Discriminators
    • Single-table inheritance via .HasDiscriminator() for asset subclasses (Computer, Monitor…).
  7. Relationships & Navigations
    • One-to-many (HasOne(...).WithMany(...)), cascade delete, navigation properties.

4. BuildTargetModel Method πŸ—οΈ

protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
    #pragma warning disable 612, 618

    modelBuilder
        .HasAnnotation("Relational:MaxIdentifierLength", 128)
        .HasAnnotation("ProductVersion", "5.0.12")
        .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

    // … entity configurations …
    
    #pragma warning restore 612, 618
}
  • ModelBuilder: EF Core’s fluent API entry point.
  • HasAnnotation: Adds provider-specific metadata (e.g., SQL Server identity strategy).
  • Entities are configured in sequential modelBuilder.Entity("FullTypeName", b => { … }) blocks.

5. Entity Configurations πŸ“‹

Below is a sample of the hundreds of properties configured.
(Full file defines ~25 EF Core entities under irby schema.)

Entity Type Table (Schema) Key Notes
IrbyImaging.DomainClasses.Asset Asset (irby) Id: uniqueidentifier Base discriminator; shared columns
β€’ Computer Asset (irby) Inherited OperatingSystemId, AssetNumber, …
β€’ MobileDevice Asset (irby) Inherited Unique Imei index; Carrier, Type
β€’ DeskPhone Asset (irby) Inherited MacAddress, PhoneNumber
IrbyImaging.DomainClasses.Employee Employee (irby) Id: uniqueidentifier Unique JdeNumber index
IrbyImaging.DomainClasses.FieldOffice FieldOffice (irby) Id Unique JdeNumber; manager FK
IrbyImaging.DomainClasses.DocumentImage DocumentImage (irby) DocumentImageId New property FileName added by migration
IrbyImaging.DomainClasses.AssetHistory AssetHistory (irby) Id Tracks asset changes
IrbyImaging.DomainClasses.Job Job (irby) Id Unique JdeNumber
… and many more

Spotlight: DocumentImage

The DocumentImage entity is the one extended by AddFileName migration.

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

This column allows the application to store the original file name of uploaded documents.


6. Entity Relationships & Inheritance πŸ“Š

Inheritance (Single-Table)

All Asset-derived classes share one physical table with a Discriminator column:

modelBuilder.Entity("IrbyImaging.DomainClasses.Asset", b => {
    b.HasDiscriminator<string>("Discriminator")
     .HasValue("Asset");
});

modelBuilder.Entity("IrbyImaging.DomainClasses.Computer", b => {
    b.HasBaseType("IrbyImaging.DomainClasses.Asset");
    b.HasDiscriminator().HasValue("Computer");
});

Sample Foreign Keys

  • Asset β†’ Employee (Owner)
  • Asset β†’ FieldOffice (FieldOfficeId)
  • AssetModelDetail β†’ AssetModel
  • Employee β†’ Job (CurrentJobId)
  • Computer β†’ OperatingSystem

7. Applying Migrations & Usage Examples πŸš€

7.1 Creating a Migration

dotnet ef migrations add AddFileName
  • Generates two files under Migrations/20220303152541_AddFileName.*.cs
    • AddFileName.cs (Up/Down methods)
    • AddFileName.Designer.cs (model snapshot)

7.2 Updating the Database

dotnet ef database update
  • Applies pending migrations to your SQL Server database.
  • Adds the FileName column to irby.DocumentImage along with any other changes.

7.3 Rolling Back

dotnet ef database update LastGoodMigrationName
  • Reverts schema to the specified migration.

8. UML Diagrams πŸ“ˆ

Below is a Mermaid class diagram illustrating Asset inheritance and key relationships:

classDiagram
    %% Base
    Asset <|-- Computer
    Asset <|-- MobileDevice
    Asset <|-- DeskPhone
    Asset <|-- Monitor
    Asset <|-- OfficePrinter
    Asset <|-- Dock
    Asset <|-- Printer
    Asset <|-- NetworkEquipment
    Asset <|-- SafetyEquipment

    %% Relationships
    Asset o-- FieldOffice : FieldOfficeId
    Asset o-- Employee : OwnerId
    Asset o-- AssetModel : ModelId

    Employee "1" o-- "*" Asset : Assets
    FieldOffice "1" o-- "*" Asset : Assets
    Job "1" o-- "*" Employee : Employees
    AssetModel "1" o-- "*" AssetModelDetail : AssetsModelDetail
    Manufacturer "1" o-- "*" AssetModel : Models

9. Conclusion 🏁

The AddFileName.Designer.cs file is a critical part of EF Core’s migrations infrastructure. It:

  • πŸ” Describes the complete EF model after this migration
  • πŸ“ˆ Enables EF to diff schema changes for future migrations
  • πŸ›‘οΈ Is automatically generated and not typically hand-modified

By understanding its structure, you gain deep insight into your database schema, entity mappings, and how EF Core orchestrates your data layer.

Happy coding! πŸš€