📚 Migration Documentation: TablesForLegalImaging
📋 Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Migration Class: TablesForLegalImaging
- Data Models 📊
- UML Class Diagram 📈
- Applying the Migration 🚀
- Error Handling & Return Types ⚠️
- API Endpoints Overview 🛠️
- Helper Methods & Utilities 🔧
- Third-Party Integrations 🔌
- Sample Code Blocks & Usage Examples 💡
- Conclusion 🎯
1. Overview & Purpose
This Entity Framework Core migration introduces two new tables—DocumentAttribute and DocumentType—into the irby schema of the database. These tables support the Legal Imaging feature by storing document metadata and document-type definitions.
Key goals:
- 📂 DocumentAttribute: Captures dynamic attributes for legal imaging documents (e.g., tags, multi‐value fields).
- 📂 DocumentType: Defines allowable types/categories of documents (e.g., “Affidavit”, “Contract”).
2. Dependencies & Technologies
| Technology | Version/Notes |
|---|---|
| .NET / C# | .NET 6+ (C# 10+) |
| Entity Framework Core | Microsoft.EntityFrameworkCore & Migrations |
| Database | SQL Server (uses uniqueidentifier, datetime2) |
| Schema | irby (must exist or be created prior to migration) |
Prerequisite: Ensure the irby schema exists, e.g.:
CREATE SCHEMA irby;
3. Migration Class: TablesForLegalImaging
namespace IrbyImaging.DbContext.Migrations
{
public partial class TablesForLegalImaging : Migration
{
protected override void Up(MigrationBuilder migrationBuilder) { … }
protected override void Down(MigrationBuilder migrationBuilder) { … }
}
}
This class inherits from Microsoft.EntityFrameworkCore.Migrations.Migration and overrides two methods to apply or revert the schema changes.
🔼 Up Method (Apply)
- Creates the
DocumentAttributeandDocumentTypetables under theirbyschema. - Defines columns, data types, nullability and primary keys.
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "DocumentAttribute",
schema: "irby",
columns: table => new
{
Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
AttributeName = table.Column<string>(type: "nvarchar(max)", nullable: true),
IsMultiValue = table.Column<bool>(type: "bit", nullable: false),
Values = table.Column<string>(type: "nvarchar(max)", nullable: true),
DocumentType = table.Column<string>(type: "nvarchar(max)", nullable: true),
IsAvailable = table.Column<bool>(type: "bit", nullable: false),
IsDeleted = table.Column<bool>(type: "bit", nullable: false),
AddedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
AddedDate = table.Column<DateTime>(type: "datetime2", nullable: false),
ModifiedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
ModifiedDate = table.Column<DateTime>(type: "datetime2", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_DocumentAttribute", x => x.Id);
});
migrationBuilder.CreateTable(
name: "DocumentType",
schema: "irby",
columns: table => new
{
Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
Type = table.Column<string>(type: "nvarchar(max)", nullable: true),
IsAvailable = table.Column<bool>(type: "bit", nullable: false),
IsDeleted = table.Column<bool>(type: "bit", nullable: false),
AddedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
AddedDate = table.Column<DateTime>(type: "datetime2", nullable: false),
ModifiedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
ModifiedDate = table.Column<DateTime>(type: "datetime2", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_DocumentType", x => x.Id);
});
}
🔽 Down Method (Revert)
- Drops both tables, rolling back the schema to its previous state.
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(name: "DocumentAttribute", schema: "irby");
migrationBuilder.DropTable(name: "DocumentType", schema: "irby");
}
4. Data Models
DocumentAttribute
| Column | Type | Nullable | Description |
|---|---|---|---|
| Id | uniqueidentifier |
No | Primary Key (GUID) |
| AttributeName | nvarchar(max) |
Yes | Name of the attribute (e.g., “CaseNumber”) |
| IsMultiValue | bit |
No | true if multiple values allowed |
| Values | nvarchar(max) |
Yes | Comma‐separated values or JSON array |
| DocumentType | nvarchar(max) |
Yes | Type(s) of documents this attribute applies to |
| IsAvailable | bit |
No | Soft‐toggle availability |
| IsDeleted | bit |
No | Soft‐delete flag |
| AddedBy | nvarchar(max) |
Yes | Username or process that added the record |
| AddedDate | datetime2 |
No | Timestamp when added |
| ModifiedBy | nvarchar(max) |
Yes | Username or process that last modified the record |
| ModifiedDate | datetime2 |
No | Timestamp of last modification |
DocumentType
| Column | Type | Nullable | Description |
|---|---|---|---|
| Id | uniqueidentifier |
No | Primary Key (GUID) |
| Type | nvarchar(max) |
Yes | Name of document type (e.g., “Contract”) |
| IsAvailable | bit |
No | Soft‐toggle availability |
| IsDeleted | bit |
No | Soft‐delete flag |
| AddedBy | nvarchar(max) |
Yes | Creator of the record |
| AddedDate | datetime2 |
No | Creation timestamp |
| ModifiedBy | nvarchar(max) |
Yes | Last modifier |
| ModifiedDate | datetime2 |
No | Last modified timestamp |
5. UML Class Diagram
classDiagram
class DocumentAttribute {
+Guid Id
+string AttributeName
+bool IsMultiValue
+string Values
+string DocumentType
+bool IsAvailable
+bool IsDeleted
+string AddedBy
+DateTime AddedDate
+string ModifiedBy
+DateTime ModifiedDate
}
class DocumentType {
+Guid Id
+string Type
+bool IsAvailable
+bool IsDeleted
+string AddedBy
+DateTime AddedDate
+string ModifiedBy
+DateTime ModifiedDate
}
6. Applying the Migration 🚀
-
Add Migration (if not already generated):
dotnet ef migrations add TablesForLegalImaging -
Update Database:
dotnet ef database update 20220607180629_TablesForLegalImaging -
Verify in SQL Server Management Studio or your DB admin tool:
- Schema:
irby - Tables:
DocumentAttribute,DocumentType
- Schema:
7. Error Handling & Return Types ⚠️
- Return Types: Both
UpandDownareprotected override void—they return nothing. - Error Handling: EF Core will throw exceptions (e.g., if schema doesn’t exist or table already exists). Wrap
database updatein your CI/CD pipeline to catch and roll back on failure.
8. API Endpoints Overview 🛠️
N/A – This migration has no direct API. It simply modifies the database schema. Any Web/API layer consuming these tables should implement CRUD endpoints for
DocumentAttributeandDocumentTypein a separate project.
9. Helper Methods & Utilities 🔧
MigrationBuilder: Core EF Core helper for building create/drop statements.- No custom utilities or extension methods are used in this migration file.
10. Third-Party Integrations 🔌
- Entity Framework Core: Handles the generation and execution of SQL statements based on this migration.
- SQL Server: Target database engine using T-SQL.
11. Sample Code Blocks & Usage Examples 💡
Seeding data for the newly created tables can be done in your
DbContext.OnModelCreatingor via a separate seeding migration.
modelBuilder.Entity<DocumentType>(entity =>
{
entity.HasData(new DocumentType
{
Id = Guid.NewGuid(),
Type = "Affidavit",
IsAvailable = true,
IsDeleted = false,
AddedBy = "system",
AddedDate = DateTime.UtcNow,
ModifiedBy = "system",
ModifiedDate = DateTime.UtcNow
});
});
Query Example (LINQ):
var activeDocTypes = await _dbContext.DocumentTypes
.Where(dt => dt.IsAvailable && !dt.IsDeleted)
.ToListAsync();
12. Conclusion 🎯
This migration lays the groundwork for Legal Imaging features by defining two essential tables within the irby schema. By following best practices—using GUIDs, soft‐delete flags, audit columns, and EF Core migrations—this design is both scalable and maintainable. After applying it, your application can store and retrieve document metadata and types seamlessly.
Next Steps:
- Implement repository/services for CRUD operations on these tables.
- Expose endpoints in your Web API layer (e.g., GET/POST
api/documenttypes).- Add validation and business logic for multi‐value attributes and type restrictions.
Generated by the IrbyImaging team 🏢