📋 IrbyImaging.DbContext Migration: 20230822130112_AddAuditTable.Designer.cs

Auto‑generated EF Core migration designer file for the AddAuditTable migration.
Contains the target model snapshot used by Entity Framework Core to build or update the database schema.


🗂 Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Key Functional Areas
  4. Data Models Registered
  5. EF Core Fluent API Mappings
  6. UML Diagram
  7. Usage & Applying the Migration
  8. Conclusion

1. Overview & Purpose

This designer file is auto‑generated by Entity Framework Core (EF Core) as part of the AddAuditTable migration.
It defines the structure of the database model (all entities, their properties, keys, indices, and relationships) as EF Core expects it after applying this migration.

  • Class: AddAuditTable
  • Namespace: IrbyImaging.DbContext.Migrations
  • Key Method:
    protected override void BuildTargetModel(ModelBuilder modelBuilder)
    
    This override instructs EF Core how to build the complete model snapshot.

Why this matters

  • Ensures consistency between your C# domain classes and the SQL Server schema.
  • Allows EF Core tools to scaffold the next migration by comparing your current domain model to this snapshot.

2. Dependencies & Technologies

Technology Version / Role
.NET 7.0.7
C# 10+
Entity Framework Core 7.0.7
Database Provider Microsoft SQL Server (SqlServerModelBuilderExtensions)
Primary Libraries Microsoft.EntityFrameworkCore, Microsoft.EntityFrameworkCore.Migrations

3. Key Functional Areas

  1. Model Annotations
    • ProductVersion: "7.0.7"
    • Relational:MaxIdentifierLength: 128
  2. Identity Columns
    • Globally enabled via UseIdentityColumns()
  3. Entity Registrations
    • Configures 100+ domain classes (e.g., Asset, Employee, AuditDetail, etc.)
    • Sets up inheritance (TPH) for Asset sub‑types (Computer, Monitor, MobileDevice…)
  4. Relationships & Navigation
    • Foreign keys, cascading behaviors, and .HasIndex() definitions
  5. Newly Added Audit Table
    • AuditDetail entity and its mapping (core of this migration)

4. Data Models Registered

4.1 AuditDetail Table 💾

This migration introduces the AuditDetail entity to capture user/audit trails (e.g., controller actions, IP, timestamps).

Table Mapping

EF Entity Schema Table Name
AuditDetail "irby" "AuditDetail"

Columns & Properties

Property CLR Type SQL Type Notes
AuditDetailId Guid uniqueidentifier Primary Key
ActionName string nvarchar(max) Action (method) name
ActivityDate DateTime datetime2 When the action occurred
Area string nvarchar(max) MVC Area (if any)
ControllerName string nvarchar(max) MVC Controller
DocumentImageId Guid? uniqueidentifier FK → DocumentImage (optional)
EmployeeId Guid? uniqueidentifier FK → Employee
IpAddress string nvarchar(max) Client IP
IsFirstLogin string nvarchar(max) True/false as string
LangId string nvarchar(max) Language identifier
LoginStatus string nvarchar(max) e.g., Success/Failure
Namespace string nvarchar(max) .NET namespace of the controller
PageAccessed string nvarchar(max) The URL path
RoleId Guid? uniqueidentifier FK → Role
SessionId string nvarchar(max) ASP.NET Session ID
UrlReferrer string nvarchar(max) HTTP Referrer header
UserId Guid uniqueidentifier ID of the user performing the action

Keys & Indexes

  • Primary Key
    b.HasKey("AuditDetailId");
    
  • Indexes
    • EmployeeId → speeds up user look‑ups
    • RoleId → quick role‑based queries

Relationships

  • EmployeeId → Employee (nullable)
  • RoleId → Role (nullable)
  • DocumentImageId → DocumentImage (nullable)

Configured via:

b.HasOne("IrbyImaging.DomainClasses.Employee", "User")
 .WithMany()
 .HasForeignKey("EmployeeId");

b.HasOne("IrbyImaging.DomainClasses.Role", "Role")
 .WithMany()
 .HasForeignKey("RoleId");

4.2 Other Entities (Summary)

The migration snapshot includes the full domain model (100+ tables). Below is a concise list of major entities:

Category Examples
Asset Management Asset, AssetModel, AssetHistory, Computer, Monitor, MobileDevice, …
User & Security Employee, Role, Permissions, LinkUserRole, LinkRolePermission
Billing & GL BillingInvoice, GeneralLedger, GLTypeDetail
Imaging & Documents DocumentImage, DocumentType, DocumentCategory, DocumentAttribute, …
Time Tracking TimeSheetEntry, TimeEntryType
Support & Tickets Ticket, Update, Issue
3rd‑Party Sync ODSEmployee, ODSJob, ODSCustomer

5. EF Core Fluent API Mappings

The BuildTargetModel method uses the Fluent API to configure:

  • Annotations
    modelBuilder
      .HasAnnotation("ProductVersion", "7.0.7")
      .HasAnnotation("Relational:MaxIdentifierLength", 128);
    SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
    
  • Entity Definitions
    • .Entity("Fully.Qualified.Clr.Type", b => { … })
    • Property mapping:
      b.Property<Guid>("Id")
       .ValueGeneratedOnAdd()
       .HasColumnType("uniqueidentifier");
      
  • Inheritance (TPH)
    • Single table for asset sub‑types (discriminator on "Discriminator" column)
  • Indexes & Keys
    b.HasKey("Id");
    b.HasIndex("SomeForeignKey");
    b.HasIndex("UniqueProperty").IsUnique().HasFilter("[Col] IS NOT NULL");
    
  • Relationships
    b.HasOne("ParentType", "NavProperty")
     .WithMany("CollectionNav")
     .HasForeignKey("ForeignKeyColumn")
     .OnDelete(DeleteBehavior.Cascade);
    

6. UML Diagram

classDiagram
    class AuditDetail {
      +Guid AuditDetailId
      +string ActionName
      +DateTime ActivityDate
      +string Area
      +string ControllerName
      +Guid? DocumentImageId
      +Guid? EmployeeId
      +string IpAddress
      +string IsFirstLogin
      +string LangId
      +string LoginStatus
      +string Namespace
      +string PageAccessed
      +Guid? RoleId
      +string SessionId
      +string UrlReferrer
      +Guid UserId
    }

    AuditDetail "0..1" <-- "1" Employee : EmployeeId
    AuditDetail "0..1" <-- "1" Role     : RoleId
    AuditDetail "0..1" <-- "1" DocumentImage : DocumentImageId

7. Usage & Applying the Migration

  1. Add the Migration
    dotnet ef migrations add AddAuditTable
    
  2. Update the Database
    dotnet ef database update
    
  3. Result
    • A new irby.AuditDetail table is created.
    • All existing schema objects remain intact.

8. Conclusion

This designer file is a critical piece of the EF Core migration pipeline. It captures the entire database model after applying the AddAuditTable migration, ensuring:

  • Consistency between your C# domain classes and SQL schema.
  • Safe evolution of the database over time.
  • Smooth scaffolding of future migrations.

Enjoy your new audit trail capabilities! 📑🔍