📝 20230626202026_AddNewTableBlobData Migration Documentation

Welcome to the detailed documentation for the 20230626202026_AddNewTableBlobData.Designer.cs migration in the IrbyImaging.DbContext project. This file is auto-generated by Entity Framework Core and represents the snapshot of your database schema after applying the migration that adds the new BlobData table.


📑 Table of Contents

  1. Overview & Purpose ❓
  2. Migration Class Setup 🏗️
  3. BuildTargetModel Method 🔍
  4. Key Table Definitions 📋
  5. Table-per-Hierarchy (TPH) Mapping 🏭
  6. Entity Relationships & Navigations 🔗
  7. Dependencies & Technologies ⚙️
  8. How to Apply This Migration 🚀
  9. Mermaid UML Diagram 🖼️
  10. Conclusion 🎯

1. Overview & Purpose ❓

This migration designer file captures the target model of your database after the AddNewTableBlobData migration. It:

  • Defines all existing entity mappings (tables, columns, keys, indexes).
  • Introduces a new table called BlobData in the irby schema.
  • Maintains Table-per-Hierarchy (TPH) mappings for inheritance on the Asset entity.

Note: This file is auto-generated by the EF Core tooling (dotnet ef migrations add ...).
Do not manually edit this file. Instead, modify your domain classes and create a new migration.


2. Migration Class Setup 🏗️

[DbContext(typeof(IrbyContext))]
[Migration("20230626202026_AddNewTableBlobData")]
partial class AddNewTableBlobData
{
    protected override void BuildTargetModel(ModelBuilder modelBuilder)
    {
        // ... model configuration ...
    }
}
  • [DbContext(typeof(IrbyContext))]
    Associates this migration with the IrbyContext.
  • [Migration("20230626202026_AddNewTableBlobData")]
    The unique identifier for this migration (timestamp + name).
  • BuildTargetModel(ModelBuilder modelBuilder)
    Captures the complete schema as method calls on modelBuilder.

3. BuildTargetModel Method 🔍

Inside BuildTargetModel, EF Core:

  1. Annotates the model with provider info (ProductVersion, MaxIdentifierLength).
  2. Configures identity/sequence strategies for SQL Server:
    SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
    
  3. Defines Entities via modelBuilder.Entity("FullTypeName", b => { ... }).

This method is the blueprint of every table, column, key, index, discriminator, and relationship.


4. Key Table Definitions 📋

The migration configures 50+ entities. Below are highlights, focusing on the new and related tables.

4.1 BlobData Table (New) 🆕

Column CLR Type SQL Type Constraints
BlobDataId Guid uniqueidentifier Primary Key
ImageContents byte[] varbinary(max)
TempDocumentDataId Guid? uniqueidentifier Nullable Foreign Key

Definition Snippet:

modelBuilder.Entity("IrbyImaging.DomainClasses.BlobData", b =>
{
    b.Property<Guid>("BlobDataId")
        .HasColumnType("uniqueidentifier");
    b.Property<byte[]>("ImageContents")
        .HasColumnType("varbinary(max)");
    b.Property<Guid?>("TempDocumentDataId")
        .HasColumnType("uniqueidentifier");

    b.HasKey("BlobDataId");
    b.ToTable("BlobData", "irby");
});
  • Location: Schema irby, table BlobData
  • Purpose: Store large binary data (blobs) such as images or documents before or after processing.

4.2 TempDocumentData Table (Referenced) 📑

This table holds temporary document metadata and may reference BlobData.

Column CLR Type SQL Type
TempDocumentDataId Guid uniqueidentifier
BlobDataId Guid? uniqueidentifier
ImageContents byte[] varbinary(max)
ImageOCRContents byte[] varbinary(max)
... ... ...

Note: While the FK column exists (BlobDataId), EF Core did not auto-configure the relationship here.
You can add a navigation in your domain class and run Add-Migration again to enforce it.


5. Table-per-Hierarchy (TPH) Mapping 🏭

The Asset base entity uses TPH inheritance to store diverse asset types in one table:

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

// Examples of derived types:
modelBuilder.Entity("IrbyImaging.DomainClasses.Computer", b => {
    b.HasBaseType("IrbyImaging.DomainClasses.Asset");
    b.HasDiscriminator().HasValue("Computer");
});
modelBuilder.Entity("IrbyImaging.DomainClasses.MobileDevice", b => {
    b.HasBaseType("IrbyImaging.DomainClasses.Asset");
    b.HasDiscriminator().HasValue("MobileDevice");
});
  • Single table named Asset in schema irby
  • Discriminator column indicates the actual CLR type (e.g., "Computer", "DeskPhone").
  • One large table simplifies queries at the expense of many nullable columns.

6. Entity Relationships & Navigations 🔗

EF Core configures foreign keys and navigations at the end of BuildTargetModel:

// Asset → FieldOffice (many-to-one)
b.HasOne("IrbyImaging.DomainClasses.FieldOffice", "FieldOffice")
 .WithMany("Assets")
 .HasForeignKey("FieldOfficeId");

// AssetModel → Manufacturer (many-to-one)
b.HasOne("IrbyImaging.DomainClasses.Manufacturer", "Make")
 .WithMany("Models")
 .HasForeignKey("MakeId")
 .OnDelete(DeleteBehavior.Cascade);

// Ticket → Update (one-to-many)
b.HasOne("IrbyImaging.DomainClasses.Ticket", null)
 .WithMany("Updates")
 .HasForeignKey("TicketId")
 .OnDelete(DeleteBehavior.Cascade);

Tip: Always verify that your domain classes include navigation properties if you need eager-loading or cascade behaviors.


7. Dependencies & Technologies ⚙️

Technology Purpose
.NET 7.0 / C# Language & runtime
Entity Framework Core 7.0.7 ORM & Migrations
Microsoft SQL Server Target relational database
EF Core Design Tools dotnet ef CLI / Package Manager

8. How to Apply This Migration 🚀

  1. Add the Migration (already done):
    dotnet ef migrations add AddNewTableBlobData
    
  2. Review the generated files:
    • 20230626202026_AddNewTableBlobData.cs
    • 20230626202026_AddNewTableBlobData.Designer.cs
  3. Update the Database:
    dotnet ef database update
    
  4. Verify the BlobData table:
    SELECT TOP 10 * FROM irby.BlobData;
    

9. Mermaid UML Diagram 🖼️

classDiagram
    class BlobData {
        +Guid BlobDataId
        +byte[] ImageContents
        +Guid? TempDocumentDataId
    }
    class TempDocumentData {
        +Guid TempDocumentDataId
        +Guid? BlobDataId
        +byte[] ImageContents
        +byte[] ImageOCRContents
    }
    BlobData "1" <-- "0..1" TempDocumentData : references

Visualize how TempDocumentData may link to BlobData for storing binary payloads separately.


10. Conclusion 🎯

This migration:

  • Adds the BlobData table for improved binary data management.
  • Maintains the existing schema snapshot for 50+ entities.
  • Utilizes SQL Server identity columns and TPH for inheritance.

By understanding the BuildTargetModel snapshot, you gain insight into the full database schema, making advanced customizations, debugging, or rollbacks much easier. Happy coding! 🚀