📑 AddAccountingDocumentType Migration Documentation


Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Migration Details
  4. Key Functional Areas
  5. Data Model Changes
  6. Usage Guide
  7. Code Snippets & Examples
  8. UML Diagram
  9. Conclusion & Best Practices

1. Overview & Purpose

File: IrbyImaging.DbContext/Migrations/20240523153517_AddAccountingDocumentType.cs
Namespace: IrbyImaging.DbContext.Migrations
Migration Name: AddAccountingDocumentType

📝 Purpose:
This Entity Framework Core migration augments the TempDocumentData and DocumentImage tables in the irby schema by adding:

  • A boolean flag (IsAccounting) to indicate whether a document is accounting-related.
  • A GUID field (AccountingDocumentType) in DocumentImage to classify the accounting document subtype.

Why?

  • To tag images/documents as accounting documents for specialized processing.
  • To support future business logic, reporting, or filtering based on document type.

2. Dependencies & Technologies

Technology / Package Purpose
Microsoft.EntityFrameworkCore ORM framework managing data access & migrations.
Microsoft.EntityFrameworkCore.Migrations Provides migration APIs (MigrationBuilder).
.NET 6.0+ C# runtime environment.

⚙️ Note: Ensure that your project references Microsoft.EntityFrameworkCore.Design and Microsoft.EntityFrameworkCore.Tools to scaffold and apply migrations.


3. Migration Details

The migration class inherits from Migration and overrides two methods:

public partial class AddAccountingDocumentType : Migration

3.1 Up Method: Adding Columns

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.AddColumn<bool>(
        name: "IsAccounting",
        schema: "irby",
        table: "TempDocumentData",
        type: "bit",
        nullable: false,
        defaultValue: false);

    migrationBuilder.AddColumn<Guid>(
        name: "AccountingDocumentType",
        schema: "irby",
        table: "DocumentImage",
        type: "uniqueidentifier",
        nullable: false,
        defaultValue: new Guid("00000000-0000-0000-0000-000000000000"));

    migrationBuilder.AddColumn<bool>(
        name: "IsAccounting",
        schema: "irby",
        table: "DocumentImage",
        type: "bit",
        nullable: false,
        defaultValue: false);
}
  • AddColumn<bool>

    • Table: TempDocumentData
    • Column: IsAccounting
    • Type: bit (non-nullable, default false)
  • AddColumn<Guid>

    • Table: DocumentImage
    • Column: AccountingDocumentType
    • Type: uniqueidentifier (non-nullable, default 00000000-0000-0000-0000-000000000000)
  • AddColumn<bool>

    • Table: DocumentImage
    • Column: IsAccounting
    • Type: bit (non-nullable, default false)

3.2 Down Method: Rolling Back

protected override void Down(MigrationBuilder migrationBuilder)
{
    migrationBuilder.DropColumn(
        name: "IsAccounting",
        schema: "irby",
        table: "TempDocumentData");

    migrationBuilder.DropColumn(
        name: "AccountingDocumentType",
        schema: "irby",
        table: "DocumentImage");

    migrationBuilder.DropColumn(
        name: "IsAccounting",
        schema: "irby",
        table: "DocumentImage");
}
  • Reverts schema changes by dropping the three columns introduced in Up.

4. Key Functional Areas

  • Document Classification
    • IsAccounting flags any document (temporary data or image) for accounting workflows.
  • Document Typing
    • AccountingDocumentType allows fine-grained categorization (e.g., invoice, receipt, voucher).

5. Data Model Changes

Table Column Data Type Nullable Default Value Description
TempDocumentData IsAccounting bit No false Indicates accounting classification.
DocumentImage AccountingDocumentType uniqueidentifier No 00000000-0000-0000-0000-000000000000 GUID for document subtype.
DocumentImage IsAccounting bit No false Flags image as accounting-related.

6. Usage Guide

6.1 Applying the Migration

  1. Add Migration (if not already scaffolded):

    dotnet ef migrations add AddAccountingDocumentType
    
  2. Update Database:

    dotnet ef database update
    

6.2 Rolling Back the Migration

  • Step Back one migration (drops the added columns):

    dotnet ef database update LastGoodMigrationName
    
  • Or use the automatic Down script:

    dotnet ef migrations remove
    

7. Code Snippets & Examples

7.1 Accessing New Properties in C# Models

public class TempDocumentData
{
    public int Id { get; set; }
    public bool IsAccounting { get; set; }  // New property
    // ... other props
}

public class DocumentImage
{
    public Guid Id { get; set; }
    public bool IsAccounting { get; set; }  // New flag
    public Guid AccountingDocumentType { get; set; } // New GUID
    // ... other props
}

7.2 Filtering Accounting Documents

// Retrieve all accounting-related document images
var accountingImages = dbContext.DocumentImages
    .Where(img => img.IsAccounting)
    .ToList();

8. UML Diagram

classDiagram
    class TempDocumentData {
        +int Id
        +bool IsAccounting
        +... other properties
    }

    class DocumentImage {
        +Guid Id
        +bool IsAccounting
        +Guid AccountingDocumentType
        +... other properties
    }

    TempDocumentData --> "0..*" DocumentImage : stores

📊 Legend:

  • + = public property
  • Arrow indicates relationship (example only)

9. Conclusion & Best Practices

🎯 Summary:
The AddAccountingDocumentType migration enhances your database schema to better support accounting-specific workflows, tagging documents and images with relevant metadata.

✅ Best Practices:

  • Back up your database before applying migrations in production.
  • Review the generated SQL (via dotnet ef migrations script) for compliance with DB policies.
  • Keep migrations small and focused to simplify rollbacks and collaboration.

Thank you for using Entity Framework Core! If you have any questions or need further customization, consult the EF Core Documentation or reach out to your dev team. 🚀