πŸ“‹ Table of Contents

  1. Overview & Purpose
  2. Key Functional Areas
  3. Dependencies & Technologies
  4. Migration: AddFormsTable
  5. Database Schema: IrbyForms Table
  6. UML Class Diagram
  7. How to Apply This Migration
  8. Usage Examples
  9. 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 the IrbyForms table with appropriate columns and a primary key.
  • Rollback Support
    Implements a Down method 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"
  • 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.IrbyForms if it exists.

πŸ“ 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

  1. Ensure EF Core Tools Installed
    dotnet tool install --global dotnet-ef
    
  2. Add the Migration (if not generated automatically)
    dotnet ef migrations add AddFormsTable \
      --project IrbyImaging.DbContext \
      --startup-project IrbyImaging.WebApi
    
  3. 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! πŸŽ‰