π Migration: Add New Column to DocumentImage (20230210162723_AddNewColumnToDocumentImage.Designer.cs)
π Table of Contents
- π Overview & Purpose
- π File Structure & Metadata
- π Migration Anatomy
- π¦ Entities & Schema Snapshot
- π Relationships & Constraints
- βοΈ Dependencies & Technologies
- π Using the Migration
- π UML Diagram Example
- π Conclusion
π Overview & Purpose
This Designer file is an autoβgenerated snapshot of your EF Core model after applying the migration AddNewColumnToDocumentImage on 2023β02β10.
It describes the shape of every table, column, index, key and relationship in your database so that EF Core can:
- Scaffold future migrations
- Perform comparisons between model versions
- Generate SQL scripts for
Up()andDown()operations
Tip: You should never edit this file manuallyβchanges here are overwritten by EF Core tooling.
π File Structure & Metadata
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace IrbyImaging.DbContext.Migrations
{
[DbContext(typeof(IrbyContext))]
[Migration("20230210162723_AddNewColumnToDocumentImage")]
partial class AddNewColumnToDocumentImage
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
// ... Entity definitions go here ...
}
}
}
| Section | Description |
|---|---|
<auto-generated /> |
Flags that this file is maintained by a tool (EF Core migration). |
DbContext(typeof(...)) |
Associates this migration with the IrbyContext. |
Migration("202302...") |
Unique ID & name of the migration (timestamp + descriptive name). |
BuildTargetModel(...) |
Core method: defines the final EF model snapshot after this migration. |
π Migration Anatomy
Migration Attributes
-
DbContextAttribute
References theIrbyContextβyour EF CoreDbContextimplementation. -
MigrationAttribute
The string (20230210162723_AddNewColumnToDocumentImage) encodes:20230210162723: UTC timestamp (YYYYMMDDHHMMSS)AddNewColumnToDocumentImage: Descriptive name
BuildTargetModel Method
- Invoked by EF Core to rebuild the
ModelBuilderstate. - Chains calls like
modelBuilder.Entity(...),.Property(...),.HasKey(...),.HasIndex(...), etc. - Captures:
- Tables
- Columns with CLR types & SQL mappings
- Primary Keys, Foreign Keys, Relationships
- Indexes and Annotations
Warning:
Regions suppressed with#pragma warning disable 612, 618allow legacy code or deprecated APIs to remain.
π¦ Entities & Schema Snapshot
Key Entities Overview
This migration snapshot defines the following entities (with their schemas in the irby schema unless noted):
| Entity Name | Table Name | Notes |
|---|---|---|
| Asset | irby.Asset |
+ Discriminator for Computer/Printer/etc. |
| AssetHistory | irby.AssetHistory |
Tracks lifecycle events |
| AssetModel | irby.AssetModel |
FK β AssetManufacturer |
| DocumentImage | irby.DocumentImage |
π Target of this migration |
| Ticket | irby.Ticket |
+ Updates navigation |
| IrbyForms | irby.IrbyForms |
Staged forms storage |
| CovidTestData | irby.CovidTestData |
Custom PK name |
| ... (many more) | ... | Omitted for brevity |
DocumentImage Entity & New Column
modelBuilder.Entity("IrbyImaging.DomainClasses.DocumentImage", b =>
{
b.Property<Guid>("DocumentImageId")
.HasColumnType("uniqueidentifier"); // PK
b.HasKey("DocumentImageId")
.HasName("PrimaryKey_DocumentImageId");
// β Existing columns (FileName, ImageContents, ...)
b.Property<byte[]>("ImageContents")
.HasColumnType("varbinary(max)");
// π **NEW COLUMN** introduced in this migration:
b.Property<string>("WorkOrder")
.HasColumnType("nvarchar(max)");
b.ToTable("DocumentImage", "irby");
});
| Column Name | CLR Type | SQL Type | Notes |
|---|---|---|---|
| DocumentImageId | Guid |
uniqueidentifier |
Primary Key |
| FileName | string |
nvarchar(max) |
Original file name |
| ImageContents | byte[] |
varbinary(max) |
Raw image bytes |
| WorkOrder | string |
nvarchar(max) |
βοΈ Newly added in this migration |
Why add
WorkOrder?
To capture a new business identifier that ties scanned documents to work orders in downstream workflows.
π Relationships & Constraints
A few representative relationships:
-
Asset β FieldOffice
modelBuilder.Entity("Asset", b => b.HasOne("FieldOffice", "FieldOffice") .WithMany("Assets") .HasForeignKey("FieldOfficeId")); -
Asset β AssetModel
b.HasOne("AssetModel", "Model") .WithMany() .HasForeignKey("ModelId") .OnDelete(DeleteBehavior.Cascade); -
Ticket β Update
modelBuilder.Entity("Update", b => b.HasOne("Ticket", null) .WithMany("Updates") .HasForeignKey("TicketId") .OnDelete(DeleteBehavior.Cascade)); -
Inheritance via Discriminator
TheAssettable uses a discriminator column (Discriminator) to represent derived types:Computer,Printer,MobileDevice,Dock, etc.
βοΈ Dependencies & Technologies
- .NET 5.0
- Entity Framework Core 5.0.12
- Microsoft.EntityFrameworkCore.SqlServer
- Microsoft.EntityFrameworkCore.Tools
These packages provide the
ModelBuilder, migrations API, SQL Server provider, and designβtime tools.
π Using the Migration
Applying & Rolling Back
- Add Migration (already done):
dotnet ef migrations add AddNewColumnToDocumentImage - Update Database (apply):
dotnet ef database update - Rollback (to previous):
dotnet ef database update [PreviousMigrationName]
Code Snippets
-
Check the Generated SQL
dotnet ef migrations script \ --from 20230210162723_PreviousMigration \ --to 20230210162723_AddNewColumnToDocumentImage -
Manual SQL Example
ALTER TABLE [irby].[DocumentImage] ADD [WorkOrder] NVARCHAR(MAX) NULL;
π UML Diagram Example
classDiagram
class DocumentImage {
+Guid DocumentImageId
+string FileName
+byte[] ImageContents
+string ImageOCRText
+bool IsLegalDocument
+string WorkOrder <<new>>
}
class IrbyContext {
+DbSet<DocumentImage> DocumentImages
+DbSet<Asset> Assets
// ...
}
IrbyContext "1" -- "0..*" DocumentImage
This diagram shows
DocumentImagewith theWorkOrderproperty highlighted.
π Conclusion
This Designer file is the heart of EF Coreβs migration mechanism. It represents the complete database model as EF Core understands it after your AddNewColumnToDocumentImage migration.
- Do not edit it manually.
- Use EF Core tools (
dotnet ef) to keep your model in sync. - Verify the new column (
WorkOrder) appears in your database and in LINQ queries againstDbContext.DocumentImages.
Happy migrating! π