π Documentation: 20230711153123_AddCategoryColumn.cs
π Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Migration Structure
- Detailed Code Walkthrough
- 4.1
UpMethod - 4.2
DownMethod
- 4.1
- Schema Impact Diagram (Mermaid)
- Usage Examples
- Rolling Back the Migration
- Best Practices & Tips
- Conclusion
π§ Overview & Purpose
File:
IrbyImaging.DbContext/Migrations/20230711153123_AddCategoryColumn.cs
This EF Core migration adds a new integer column named Category (with a default value of 0) to multiple tables in the "irby" schema:
TempDocumentDataDocumentTypeDocumentImageDocumentCategoryDocumentAttribute
Why?
Introducing aCategoryfield allows you to classify or group documents, images, types, attributes, etc., under a shared numeric category for filtering, reporting, or business rules.
βοΈ Dependencies & Technologies
| Technology | Version / Notes |
|---|---|
| .NET Core / .NET 6+ | Target framework for EF Core |
| Microsoft.EntityFrameworkCore | ORM library providing Migrations API |
| Microsoft.EntityFrameworkCore.Tools | CLI tooling for migrations |
| C# | Language of the migration class |
CLI Prerequisites:
dotnet efglobal tool installed- A valid
DbContextconfigured to use the"irby"schema
ποΈ Migration Structure
public partial class AddCategoryColumn : Migration
{
protected override void Up(MigrationBuilder migrationBuilder) { /* ... */ }
protected override void Down(MigrationBuilder migrationBuilder) { /* ... */ }
}
AddCategoryColumn
Partial class that inherits fromMigration.Up
Defines schema changes to apply (adding columns).Down
Defines how to revert changes (dropping columns).
π Detailed Code Walkthrough
4.1 Up Method
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "Category",
schema: "irby",
table: "TempDocumentData",
type: "int",
nullable: false,
defaultValue: 0);
// Repeat for other tables...
migrationBuilder.AddColumn<int>(
name: "Category",
schema: "irby",
table: "DocumentType",
type: "int", nullable: false, defaultValue: 0);
migrationBuilder.AddColumn<int>(
name: "Category",
schema: "irby",
table: "DocumentImage",
type: "int", nullable: false, defaultValue: 0);
migrationBuilder.AddColumn<int>(
name: "Category",
schema: "irby",
table: "DocumentCategory",
type: "int", nullable: false, defaultValue: 0);
migrationBuilder.AddColumn<int>(
name: "Category",
schema: "irby",
table: "DocumentAttribute",
type: "int", nullable: false, defaultValue: 0);
}
AddColumn<int>
Adds a non-nullableintcolumn namedCategory.defaultValue: 0
Ensures existing rows get0so the migration won't fail on non-null constraint.
4.2 Down Method
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "Category",
schema: "irby",
table: "TempDocumentData");
// Repeat for other tables...
migrationBuilder.DropColumn(name: "Category", schema: "irby", table: "DocumentType");
migrationBuilder.DropColumn(name: "Category", schema: "irby", table: "DocumentImage");
migrationBuilder.DropColumn(name: "Category", schema: "irby", table: "DocumentCategory");
migrationBuilder.DropColumn(name: "Category", schema: "irby", table: "DocumentAttribute");
}
DropColumn
Safely removes theCategorycolumn from each table, reverting schema to its previous state.
π Schema Impact Diagram (Mermaid)
erDiagram
TempDocumentData {
int Id PK
int Category
/* other fields... */
}
DocumentType {
int Id PK
int Category
}
DocumentImage {
int Id PK
int Category
}
DocumentCategory {
int Id PK
int Category
}
DocumentAttribute {
int Id PK
int Category
}
This diagram shows each table receiving a new Category attribute.
π οΈ Usage Examples
-
Add & Apply Migration
dotnet ef migrations add AddCategoryColumn --context IrbyDbContext dotnet ef database update --context IrbyDbContext -
Querying by Category
var invoices = dbContext.DocumentType .Where(dt => dt.Category == 5) .ToList(); -
Updating Category
var docImage = await dbContext.DocumentImage.FindAsync(123); docImage.Category = 2; await dbContext.SaveChangesAsync();
π Rolling Back the Migration
To revert this migration:
dotnet ef database update <PreviousMigrationName> --context IrbyDbContext
This will invoke the Down method, dropping all Category columns.
π‘ Best Practices & Tips
- Backup Database: Always backup before applying schema changes.
- Seed Default Values: If you need meaningful defaults (not just
0), consider a customUPDATESQL in theUpmethod. - Test Locally: Run migrations on a local DB to catch errors.
- Migration Order: Ensure no dependent code references
Categorybefore the migration is applied.
π Conclusion
This migration extends your data model by adding a uniform Category column across key document-related tables. Itβs a straightforward schema update that unlocks new filtering, grouping, and business logic capabilities. By following the usage examples and best practices, you can seamlessly integrate and manage this change in your EF Core workflow. π
Generated by EF Core Migrations β’ Β© IrbyImaging