π Table of Contents
- Overview & Purpose
- Key Functional Areas
- Dependencies & Technologies
- Migration: AddFormsTable
- Database Schema: IrbyForms Table
- UML Class Diagram
- How to Apply This Migration
- Usage Examples
- Conclusion
π 1. Overview & Purpose
This documentation covers the Entity Framework Core migration AddFormsTable located in the file:
IrbyImaging.DbContext/Migrations/20220520152658_AddFormsTable.cs
Purpose
The migration introduces a new table, IrbyForms, under the schema irby. Itβs designed to store form definitions and their associated image contents in a centralized database for the Irby Imaging system.
- π Table Name:
irby.IrbyForms - π Primary Key:
FormId(GUID) - π Use Case: Persist scanned form images, metadata, and audit information.
π 2. Key Functional Areas
- Table Creation
Adds theIrbyFormstable with appropriate columns and a primary key. - Rollback Support
Implements aDownmethod to drop the table if the migration is rolled back. - Schema Management
Ensures the table resides in the"irby"schema to group imaging-related entities.
π οΈ 3. Dependencies & Technologies
| Component | Version / Notes |
|---|---|
| .NET / C# | .NET 5+ / C# 9+ |
| Entity Framework Core (EF Core) | 5.x / 6.x |
| EF Core Tools | dotnet ef CLI or Package Manager Console |
| Database Provider | SQL Server (uses uniqueidentifier, varbinary, datetime2) |
ποΈ 4. Migration: AddFormsTable
public partial class AddFormsTable : Migration
{
protected override void Up(MigrationBuilder migrationBuilder) { /* ... */ }
protected override void Down(MigrationBuilder migrationBuilder) { /* ... */ }
}
πΌ Up Method
The Up method defines all operations to bring the database forward to include the new table.
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "IrbyForms",
schema: "irby",
columns: table => new
{
FormId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
FormNumber = table.Column<string>(type: "nvarchar(max)", nullable: true),
FormName = table.Column<string>(type: "nvarchar(max)", nullable: true),
ImageContents = table.Column<byte[]>(type: "varbinary(max)", nullable: true),
DocumentUploadedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
DocumentUploadedDate = table.Column<DateTime>(type: "datetime2", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_IrbyForms", x => x.FormId);
});
}
- CreateTable
- name:
"IrbyForms" - schema:
"irby"
- name:
- Columns
FormId(GUID, PK, not null)FormNumber(string)FormName(string)ImageContents(byte[])DocumentUploadedBy(string)DocumentUploadedDate(DateTime, not null)
π½ Down Method
The Down method reverses the changes introduced in Up, enabling clean rollbacks.
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "IrbyForms",
schema: "irby");
}
- DropTable
- Removes
irby.IrbyFormsif it exists.
- Removes
π 5. Database Schema: IrbyForms Table
| Column | Data Type | Nullable | Description |
|---|---|---|---|
| FormId | uniqueidentifier | No | Primary Key (GUID) |
| FormNumber | nvarchar(max) | Yes | Internal reference number |
| FormName | nvarchar(max) | Yes | Name/label of the form |
| ImageContents | varbinary(max) | Yes | Raw binary content of the image |
| DocumentUploadedBy | nvarchar(max) | Yes | Username or identifier of uploader |
| DocumentUploadedDate | datetime2 | No | Date/time document was uploaded |
π 6. UML Class Diagram
classDiagram
class AddFormsTable {
+Up(MigrationBuilder)
+Down(MigrationBuilder)
}
class MigrationBuilder {
+CreateTable(name, schema, columns, constraints)
+DropTable(name, schema)
}
AddFormsTable --> MigrationBuilder : uses
βοΈ 7. How to Apply This Migration
- Ensure EF Core Tools Installed
dotnet tool install --global dotnet-ef - Add the Migration (if not generated automatically)
dotnet ef migrations add AddFormsTable \ --project IrbyImaging.DbContext \ --startup-project IrbyImaging.WebApi - Update the Database
dotnet ef database update \ --project IrbyImaging.DbContext \ --startup-project IrbyImaging.WebApi
π» 8. Usage Examples
1. Inserting a New Form
using (var context = new IrbyDbContext(options))
{
var form = new IrbyForm
{
FormId = Guid.NewGuid(),
FormNumber = "F-2022-001",
FormName = "Inspection Report",
ImageContents = File.ReadAllBytes("report.png"),
DocumentUploadedBy = "jdoe",
DocumentUploadedDate = DateTime.UtcNow
};
context.IrbyForms.Add(form);
context.SaveChanges();
}
2. Querying Forms
var recentForms = context.IrbyForms
.Where(f => f.DocumentUploadedDate >= DateTime.UtcNow.AddDays(-7))
.OrderByDescending(f => f.DocumentUploadedDate)
.ToList();
3. Rolling Back the Migration
dotnet ef database update 20220520150000_PreviousMigration
π 9. Conclusion
The AddFormsTable migration is a fundamental step in extending the Irby Imaging database to persist form metadata and binary image contents. By following the steps in this document, you can:
- Seamlessly apply the new table to your database.
- Roll back if needed.
- Integrate CRUD operations in your application layer.
Feel free to reference this guide whenever you need to understand, apply, or revert this migration! π