๐๏ธ IrbyImaging.DbContext Migration
File: IrbyImaging.DbContext/Migrations/20230710174948_AddIsHrColumn.cs
Purpose: Adds IsHr flags and HrDocumentType to documentโrelated tables.
๐ Table of Contents
- Overview & Purpose
- Schema Changes
- Migration Details
- Dependencies & Technologies
- Data Models Affected
- Usage & Execution
- Rollback (Down Method)
- UML Diagram of Changes
- Sample CLI & Code Examples
- Conclusion
๐ 1. Overview & Purpose
This Entity Framework Core migration adds a new boolean flag (IsHr) to several tables under the irby schema and introduces a GUID column (HrDocumentType) to the DocumentImage table.
- Why? To mark whether a document entity or category is related to Human Resources (HR) processes.
- When? During database evolution when the application needs to differentiate HR-related documents for filtering, security, or business logic.
๐ ๏ธ 2. Schema Changes
| Table Name | New Column | Type | Default Value | Nullable? |
|---|---|---|---|---|
TempDocumentData |
IsHr |
bit (bool) |
false |
No |
DocumentType |
IsHr |
bit (bool) |
false |
No |
DocumentImage |
IsHr |
bit (bool) |
false |
No |
DocumentImage |
HrDocumentType |
uniqueidentifier |
00000000-0000-0000-0000-000000000000 |
No |
DocumentCategory |
IsHr |
bit (bool) |
false |
No |
DocumentAttribute |
IsHr |
bit (bool) |
false |
No |
๐ 3. Migration Details
namespace IrbyImaging.DbContext.Migrations
{
public partial class AddIsHrColumn : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
// Add IsHr to TempDocumentData
migrationBuilder.AddColumn<bool>(
name: "IsHr",
schema: "irby",
table: "TempDocumentData",
type: "bit",
nullable: false,
defaultValue: false);
// Add IsHr to DocumentType
migrationBuilder.AddColumn<bool>(
name: "IsHr",
schema: "irby",
table: "DocumentType",
type: "bit",
nullable: false,
defaultValue: false);
// Add columns to DocumentImage
migrationBuilder.AddColumn<Guid>(
name: "HrDocumentType",
schema: "irby",
table: "DocumentImage",
type: "uniqueidentifier",
nullable: false,
defaultValue: Guid.Empty);
migrationBuilder.AddColumn<bool>(
name: "IsHr",
schema: "irby",
table: "DocumentImage",
type: "bit",
nullable: false,
defaultValue: false);
// Add IsHr to DocumentCategory
migrationBuilder.AddColumn<bool>(
name: "IsHr",
schema: "irby",
table: "DocumentCategory",
type: "bit",
nullable: false,
defaultValue: false);
// Add IsHr to DocumentAttribute
migrationBuilder.AddColumn<bool>(
name: "IsHr",
schema: "irby",
table: "DocumentAttribute",
type: "bit",
nullable: false,
defaultValue: false);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
// Reverse additions
migrationBuilder.DropColumn(name: "IsHr", schema: "irby", table: "TempDocumentData");
migrationBuilder.DropColumn(name: "IsHr", schema: "irby", table: "DocumentType");
migrationBuilder.DropColumn(name: "HrDocumentType", schema: "irby", table: "DocumentImage");
migrationBuilder.DropColumn(name: "IsHr", schema: "irby", table: "DocumentImage");
migrationBuilder.DropColumn(name: "IsHr", schema: "irby", table: "DocumentCategory");
migrationBuilder.DropColumn(name: "IsHr", schema: "irby", table: "DocumentAttribute");
}
}
}
Key Points:
- Up() applies schema changes.
- Down() reverts them, ensuring safe rollbacks.
๐ฆ 4. Dependencies & Technologies
- .NET 6+ / .NET Core
- Entity Framework Core (
Microsoft.EntityFrameworkCore.Migrations) - SQL Server (implicit, due to
bitanduniqueidentifiertypes) - C# 10 (nullable enabled)
๐๏ธ 5. Data Models Affected
Although the migration file itself doesnโt contain the DbSet or entity definitions, these tables align with C# models such as:
public class DocumentType
{
public Guid Id { get; set; }
public string Name { get; set; }
public bool IsHr { get; set; } // <โ New property
}
public class DocumentImage
{
public Guid Id { get; set; }
public byte[] Data { get; set; }
public bool IsHr { get; set; } // <โ New property
public Guid HrDocumentType { get; set; } // <โ New property
}
Tip: Update your EF Core model classes to include the new properties before adding or applying the migration.
โ 6. Usage & Execution
-
Add the migration
dotnet ef migrations add AddIsHrColumn -
Apply to database
dotnet ef database update -
Verify
- Inspect the database:
SELECT TOP 5 IsHr FROM irby.DocumentType; - Ensure default
IsHr = 0(false).
- Inspect the database:
๐ 7. Rollback (Down Method)
If something goes wrong, roll back to the previous state:
dotnet ef database update <PreviousMigrationName>
This invokes the Down() method and drops all IsHr and HrDocumentType columns.
๐ 8. UML Diagram of Changes
@startuml
class TempDocumentData {
+bool IsHr
}
class DocumentType {
+bool IsHr
}
class DocumentImage {
+bool IsHr
+Guid HrDocumentType
}
class DocumentCategory {
+bool IsHr
}
class DocumentAttribute {
+bool IsHr
}
@enduml
Illustrates the new properties in each entity.
๐ง 9. Sample CLI & Code Examples
Example: Creating a new HR document
var newDocType = new DocumentType {
Id = Guid.NewGuid(),
Name = "Employee Handbook",
IsHr = true
};
dbContext.DocumentTypes.Add(newDocType);
dbContext.SaveChanges();
Filtering HR documents
var hrDocs = dbContext.DocumentImage
.Where(di => di.IsHr)
.ToList();
๐ 10. Conclusion
With AddIsHrColumn, the database now supports toggling the HRโrelated status of documents, types, categories, and attributes. This enhancement lays the groundwork for HR-specific features like:
- Access Control
- Reporting/Analytics
- Document Workflows
๐ After deploying, remember to update your application code to leverage the new
IsHrflags andHrDocumentTypefor richer, more secure HR functionality!
End of Documentation
IrbyImaging | Migration & Data Layer | ยฉ 2023