π Table of Contents
- Overview & Purpose
- Migration Responsibility
- Dependencies & Technologies
- Key Functional Areas
- Generated Model Snapshot
- Entity Relationship Diagram (UML)
- Using the Migration
- Sample Code Blocks
- Conclusion & Best Practices
1. Overview & Purpose π
This file:
IrbyImaging.DbContext/Migrations/20230620124309_AddIndexTagColumnToDocumentImage.Designer.cs
is an auto-generated EF Core migration snapshot. It captures the entire database model at the moment you ran:
dotnet ef migrations add AddIndexTagColumnToDocumentImage
Its primary purpose is to let Entity Framework Core compute the differences between your current model and the next migration. In particular, this migration adds a new column IndexTag to the DocumentImage table in the irby schema.
Note: You should never modify
.Designer.csfiles by handβthey are regenerated whenever you add or remove migrations.
2. Migration Responsibility π
- Name:
AddIndexTagColumnToDocumentImage - DbContext:
IrbyContext - Timestamp:
2023-06-20T12:43:09 - Operation:
- Detect schema changes (new property on domain class).
- Generate SQL to ALTER TABLE and ADD COLUMN.
- Maintain a snapshot of the entire EF Core model for future diffs.
3. Dependencies & Technologies βοΈ
| Technology | Version / Notes |
|---|---|
| .NET SDK | 7.0.7 (ProductVersion) |
| Entity Framework Core (EF Core) | Microsoft.EntityFrameworkCore, Migrations, Metadata |
| Database Provider | Microsoft.EntityFrameworkCore.SqlServer |
| C# Language | C# 10+ (.NET 7) |
| UML / Diagramming (Mermaid) | For class diagrams |
4. Key Functional Areas ποΈ
- Model Annotations
ProductVersionRelational:MaxIdentifierLength
- Identity Columns Configuration
UseIdentityColumns()globallyUseIdentityColumn()for specific properties
- Entities & Properties
- All domain classes (
Asset,DocumentImage,Employee, etc.)
- All domain classes (
- Indexes & Keys
- Primary keys (
HasKey) - Foreign keys (
HasForeignKey) - Unique indexes (
IsUnique()) - Standard indexes (
HasIndex)
- Primary keys (
- Inheritance (TPH)
- Discriminator column for
Assethierarchy
- Discriminator column for
- Schema & Table Mapping
- Default schema:
"irby" - Some tables without explicit schema
- Default schema:
5. Generated Model Snapshot π
Below is a highβlevel view of how EF Core reconstructs the model using ModelBuilder. The most important snippet is the DocumentImage entity where IndexTag was added.
DocumentImage Entity & IndexTag Addition
modelBuilder.Entity("IrbyImaging.DomainClasses.DocumentImage", b =>
{
// Primary key
b.HasKey("DocumentImageId")
.HasName("PrimaryKey_DocumentImageId");
// Table mapping
b.ToTable("DocumentImage", "irby");
// Existing columns
b.Property<Guid>("DocumentImageId").HasColumnType("uniqueidentifier");
b.Property<string>("FileName").HasColumnType("nvarchar(max)");
b.Property<byte[]>("ImageContents").HasColumnType("varbinary(max)");
// ... many other columns omitted for brevity ...
// π Newly added column in this migration
b.Property<string>("IndexTag")
.HasColumnType("nvarchar(max)");
// Other existing columns
b.Property<bool>("IsLegalDocument").HasColumnType("bit");
b.Property<int>("Page").HasColumnType("int");
// ... etc ...
});
What Changed?
IndexTag(typenvarchar(max)) was not present before.- This migration ensures the database column is created.
Other Entities at a Glance
| Entity | Table Name | Schema | Notes |
|---|---|---|---|
| Asset | Asset | irby | TPH hierarchy with Discriminator |
| AssetModel | AssetModel | irby | FK β Manufacturer (MakeId) |
| AssetHistory | AssetHistory | irby | Audit history |
| DocumentAttribute | DocumentAttribute | irby | Document metadata definitions |
| DocumentCategory | DocumentCategory | irby | |
| DocumentType | DocumentType | irby | |
| Employee | Employee | irby | Unique index on JdeNumber |
| FieldOffice | FieldOffice | irby | Multiple FKs to Employee |
| β¦ | β¦ | β¦ | Many other domain classes (Job, Customer, Ticket, IPMMigration, etc.) |
6. Entity Relationship Diagram (UML) π¨
Below is a simplified Mermaid diagram highlighting the DocumentImage and its relationship to an imaginary DocumentCategory entity.
classDiagram
class DocumentImage {
+Guid DocumentImageId
+string FileName
+string IndexTag
+int Page
+bool IsLegalDocument
// ... other props
}
class DocumentCategory {
+Guid Id
+string Name
+string Description
}
DocumentImage --> DocumentCategory : DocumentCategoryId
Tip: You can convert this snippet using any Mermaid Live Editor to get a visual diagram.
7. Using the Migration π οΈ
-
Define the new property in your domain class (
DocumentImage):namespace IrbyImaging.DomainClasses { public class DocumentImage { public Guid DocumentImageId { get; set; } // β¦ existing props β¦ public string IndexTag { get; set; } // π newly added } } -
Add a migration:
dotnet ef migrations add AddIndexTagColumnToDocumentImage -
Inspect & commit the two generated files:
20230620124309_AddIndexTagColumnToDocumentImage.cs20230620124309_AddIndexTagColumnToDocumentImage.Designer.cs
-
Update the database:
dotnet ef database update
8. Sample Code Blocks π¦
BuildTargetModel Snippet
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
// Global annotations
modelBuilder
.HasAnnotation("ProductVersion", "7.0.7")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
// Enable SQL Server identity columns
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
// β¦ entity definitions β¦
modelBuilder.Entity("IrbyImaging.DomainClasses.DocumentImage", b =>
{
b.Property<string>("IndexTag").HasColumnType("nvarchar(max)");
// β¦ rest β¦
});
#pragma warning restore 612, 618
}
Applying the Migration in Code
# Add migration
dotnet ef migrations add AddIndexTagColumnToDocumentImage
# Inspect changes
git diff Migrations/20230620124309_AddIndexTagColumnToDocumentImage.*
# Apply to local DB
dotnet ef database update
9. Conclusion & Best Practices β
- Keep migrations small & focused: One schema change per migration.
- Review auto-generated code: Ensure the snapshot represents your intended model.
- Commit both
.cs&.Designer.cs: They work together. - Never hand-edit generated files. Change your domain classes and re-scaffold.
- Use CI/CD pipelines to apply migrations automatically on staging/production.
β¨ With this migration in place, you can now store and query an
IndexTagfor each document image, enabling richer search, categorization, or OCR workflows in your imaging application.
Happy coding! π