π IrbyImaging.DbContext.Migrations/20240605163427_AddImagingTypes2.cs Documentation
π Table of Contents
- Overview & Purpose
- Key Functional Areas
- Dependencies & Technologies
- Data Model Changes
- Migration Workflow
- Error Handling & Return Types
- API Endpoints Overview
- Thirdβparty Integrations
- Class Diagram (UML)
- Sample Code Blocks
- Usage Examples
- Conclusion
π§ Overview & Purpose
File:
IrbyImaging.DbContext/Migrations/20240605163427_AddImagingTypes2.cs
This EF Core Migration adds two new boolean flagsβIsAccounting and IsBiddingβto two existing tables in the irby schema:
- DocumentType
- DocumentAttribute
These flags allow the application to categorize document types and attributes according to their involvement in accounting or bidding processes.
π Key Functional Areas
- Up(MigrationBuilder)
- Adds non-nullable
bitcolumns with a default offalse. - Schema:
irby, Tables:DocumentType,DocumentAttribute.
- Adds non-nullable
- Down(MigrationBuilder)
- Drops the added columns if the migration is rolled back.
π Dependencies & Technologies
| Component | Version / Tech |
|---|---|
| .NET SDK | β₯ .NET 6.0 |
| Entity Framework Core | β₯ 6.x |
| Database Provider | Microsoft SQL Server (bit) |
| C# Language | 10+ |
πΎ Data Model Changes
Before Migration
| Table | Columns (Partial) |
|---|---|
| DocumentType | Id, Name, Description |
| DocumentAttribute | Id, Name, Value |
After Migration
| Table | New Columns |
|---|---|
| DocumentType | IsAccounting (bit, not null, default: false)IsBidding (bit, not null, default: false) |
| DocumentAttribute | IsAccounting (bit, not null, default: false)IsBidding (bit, not null, default: false) |
βοΈ Migration Workflow
- Add Migration
dotnet ef migrations add AddImagingTypes2 --project IrbyImaging.DbContext - Apply Migration
dotnet ef database update --project IrbyImaging.DbContext - Rollback (if needed)
dotnet ef database update LastGoodMigration
π‘ Error Handling & Return Types
- Both Up and Down methods return
voidand rely on EF Core to throw exceptions (e.g.,SqlException) if the DDL operation fails. - Typical failure modes:
- Column already exists / does not exist.
- Schema/table not found.
- Insufficient DB permissions.
π API Endpoints Overview
This migration file does not define any API endpoints.
However, the new flags can be surfaced in your Web API Controllers or GraphQL resolvers, for example:
// Example: Filtering document types in a controller
[HttpGet("api/document-types/accounting")]
public async Task<IActionResult> GetAccountingDocumentTypes()
{
var types = await _context.DocumentTypes
.Where(dt => dt.IsAccounting)
.ToListAsync();
return Ok(types);
}
π Thirdβparty Integrations
- Entity Framework Core Migrations: Manages schema evolution.
- Microsoft SQL Server: Uses the
bittype for boolean flags.
π Class Diagram (UML)
classDiagram
class Migration {
<<abstract>>
+void Up(MigrationBuilder)
+void Down(MigrationBuilder)
}
class AddImagingTypes2 {
+override void Up(MigrationBuilder)
+override void Down(MigrationBuilder)
}
AddImagingTypes2 --|> Migration
class DocumentType {
+int Id
+string Name
+string Description
+bool IsAccounting
+bool IsBidding
}
class DocumentAttribute {
+int Id
+string Name
+string Value
+bool IsAccounting
+bool IsBidding
}
π Legend
β<<abstract>>: Abstract base class
β--|>: Inheritance
β+: Public member
π» Sample Code Blocks
Migration Class
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace IrbyImaging.DbContext.Migrations
{
public partial class AddImagingTypes2 : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
// Add flags to DocumentType
migrationBuilder.AddColumn<bool>(
name: "IsAccounting",
schema: "irby",
table: "DocumentType",
type: "bit",
nullable: false,
defaultValue: false);
migrationBuilder.AddColumn<bool>(
name: "IsBidding",
schema: "irby",
table: "DocumentType",
type: "bit",
nullable: false,
defaultValue: false);
// Add flags to DocumentAttribute
migrationBuilder.AddColumn<bool>(
name: "IsAccounting",
schema: "irby",
table: "DocumentAttribute",
type: "bit",
nullable: false,
defaultValue: false);
migrationBuilder.AddColumn<bool>(
name: "IsBidding",
schema: "irby",
table: "DocumentAttribute",
type: "bit",
nullable: false,
defaultValue: false);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(name: "IsAccounting", schema: "irby", table: "DocumentType");
migrationBuilder.DropColumn(name: "IsBidding", schema: "irby", table: "DocumentType");
migrationBuilder.DropColumn(name: "IsAccounting", schema: "irby", table: "DocumentAttribute");
migrationBuilder.DropColumn(name: "IsBidding", schema: "irby", table: "DocumentAttribute");
}
}
}
π Usage Examples
- Querying by New Flags
// Get all bidding-related document attributes var biddingAttributes = await context.DocumentAttributes .Where(attr => attr.IsBidding) .ToListAsync(); - Updating Flags at Runtime
var docType = await context.DocumentTypes.FindAsync(typeId); docType.IsAccounting = true; await context.SaveChangesAsync();
π Conclusion
The AddImagingTypes2 migration enriches your database schema by introducing two flagsβIsAccounting and IsBiddingβto both DocumentType and DocumentAttribute. These boolean indicators enable the application to easily filter, organize, and process imaging documents based on their business context (accounting vs. bidding). By following the standard EF Core migration workflow, you can seamlessly apply these changes and start leveraging the new flags across your services and APIs.
π Next Steps:
- Update your services/controllers to make use of the new flags.
- Add appropriate indexes if you expect heavy filtering on these columns.
- Write integration tests to ensure correct behavior when flags change.
Happy Coding! πβ¨