π Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Key Functional Areas
- Migration Class Breakdown
- 4.1 Up Method
- 4.2 Down Method
- Schema Changes Summary
- Sample Usage & Code Snippets
- UML Class Diagram
- Error Handling & Return Types
- Conclusion
π§ Overview & Purpose
File:
IrbyImaging.DbContext/Migrations/20230606160443_AddReviewColumnToAsset.cs
What Is This?
This C# class is an Entity Framework Core Migration. EF Core Migrations allow you to incrementally evolve your database schema by describing how to apply and roll back changes in code.
Why It Exists
- Add a new flag (
NeedsReview) to theAssettable so that each asset record can be marked for manual review. - Ensure existing data remains consistent by providing a default value (
false).
π οΈ Dependencies & Technologies
| Technology | Purpose |
|---|---|
| .NET 6+ | Platform for C# and EF Core |
| Entity Framework Core | ORM for database migrations & schema updates |
| SQL Server (or any βbitβ-supporting RDBMS) | Underlying database |
- Namespace:
Microsoft.EntityFrameworkCore.Migrations - Schema:
"irby" - Table:
"Asset"
π Key Functional Areas
- Schema Evolution
- Adds and removes a boolean column in the database table.
- Version Control
- Migration files track changes over time, enabling rollbacks.
- Data Integrity
- Ensures new column defaults to
falsefor existing records.
- Ensures new column defaults to
π§© Migration Class Breakdown
namespace IrbyImaging.DbContext.Migrations
{
public partial class AddReviewColumnToAsset : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
// Code to apply schema changes
}
protected override void Down(MigrationBuilder migrationBuilder)
{
// Code to revert schema changes
}
}
}
4.1 Up Method
- Signature:
protected override void Up(MigrationBuilder migrationBuilder) - Responsibility: Applies changes to the database when this migration is run.
migrationBuilder.AddColumn<bool>(
name: "NeedsReview",
schema: "irby",
table: "Asset",
type: "bit",
nullable: false,
defaultValue: false);
| Parameter | Description |
|---|---|
name |
Name of the new column (NeedsReview) |
schema |
Database schema (irby) |
table |
Table to modify (Asset) |
type |
SQL data type (bit) |
nullable |
Whether the column allows NULL (false) |
defaultValue |
Default value for existing rows (false) |
4.2 Down Method
- Signature:
protected override void Down(MigrationBuilder migrationBuilder) - Responsibility: Rolls back the
Upchanges when you revert this migration.
migrationBuilder.DropColumn(
name: "NeedsReview",
schema: "irby",
table: "Asset");
ποΈ Schema Changes Summary
| Action | Schema | Table | Column | Type | Nullable | Default |
|---|---|---|---|---|---|---|
| Add Column | irby | Asset | NeedsReview | bit | No | false |
| Drop Column | irby | Asset | NeedsReview | β | β | β |
π‘ Sample Usage & Code Snippets
-
Add & Apply Migration
# From the Package Manager Console Add-Migration AddReviewColumnToAsset -Context IrbyDbContext Update-Database -Context IrbyDbContext -
EF Core CLI
dotnet ef migrations add AddReviewColumnToAsset dotnet ef database update -
Querying in Code
using var context = new IrbyDbContext(); var assetsNeedingReview = context.Assets .Where(a => a.NeedsReview) .ToList();
π UML Class Diagram
@startuml
package IrbyImaging.DbContext.Migrations {
class AddReviewColumnToAsset {
+Up(MigrationBuilder)
+Down(MigrationBuilder)
}
}
Migration <|-- AddReviewColumnToAsset
@enduml
β οΈ Error Handling & Return Types
- EF Core Migrations do not return values.
- Errors (e.g., column already exists) surface as runtime exceptions from SQL Server / EF Core.
- Best practice: Enclose
Update-Databasein a try/catch when automating.
β Conclusion
This migration enhances the Asset table by introducing a boolean flag NeedsReview.
- Forward (Up): Adds the column w/ default
false. - Backward (Down): Removes the column if rolled back.
By running this migration, you can now programmatically mark and query assets that require manual review, adding an important piece of workflow metadata to your imaging solution. π
End of documentation for 20230606160443_AddReviewColumnToAsset.cs