ποΈ Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Migration Class Structure
- Schema Modification π§
- Code Implementation
- Usage Examples π
- UML Diagram πΊοΈ
- Rollback Strategy π
- Conclusion π―
1. Overview & Purpose
File:
IrbyImaging.DbContext/Migrations/20230629190543_AddNewColumnsBilling.cs
This Entity Framework Core Migration adds two new nullable string columnsβCarrierName and EmployeeNameβto the BillingInvoice table in the irby schema.
Why?
- π¦ Enhance billing records with humanβreadable references (carrier & employee).
- π Facilitate reporting and auditing by capturing who processed and which carrier handled each invoice.
2. Dependencies & Technologies
| Category | Details |
|---|---|
| Framework | .NET 6+, .NET Core |
| ORM | Entity Framework Core (EF Core) |
| Migration Tooling | Microsoft.EntityFrameworkCore.Migrations |
| Database (target) | SQL Server (uses nvarchar(max)) |
| Project Structure (DC) | IrbyImaging.DbContext |
3. Migration Class Structure
namespace IrbyImaging.DbContext.Migrations
{
/// <inheritdoc />
public partial class AddNewColumnsBilling : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
// Adds columns
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
// Drops columns
}
}
}
Key Methods
- Up: Defines the schema changes to apply.
- Down: Reverts those changes on rollback.
4. Schema Modification π§
New Columns in irby.BillingInvoice
| Column Name | SQL Type | Nullable | Description |
|---|---|---|---|
| CarrierName | nvarchar(max) | Yes | Name of the carrier/service provider |
| EmployeeName | nvarchar(max) | Yes | Name of the employee responsible for the invoice |
5. Code Implementation
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace IrbyImaging.DbContext.Migrations
{
/// <inheritdoc />
public partial class AddNewColumnsBilling : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "CarrierName",
schema: "irby",
table: "BillingInvoice",
type: "nvarchar(max)",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "EmployeeName",
schema: "irby",
table: "BillingInvoice",
type: "nvarchar(max)",
nullable: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "CarrierName",
schema: "irby",
table: "BillingInvoice");
migrationBuilder.DropColumn(
name: "EmployeeName",
schema: "irby",
table: "BillingInvoice");
}
}
}
Highlights
AddColumn<string>: Adds a nullable string column.DropColumn: Safely removes columns when rolling back.
6. Usage Examples π
1. Add Migration
dotnet ef migrations add AddNewColumnsBilling
This generates the above file with the timestamp prefix 20230629190543.
2. Apply to Database
dotnet ef database update
This will execute the Up method and update your schema.
3. Rollback Migration
dotnet ef database update LastGoodMigrationName
This rolls back by executing the Down method.
7. UML Diagram πΊοΈ
+------------------------------------------------+
| AddNewColumnsBilling (Migration) |
+------------------------------------------------+
| + Up(migrationBuilder: MigrationBuilder) |
| + Down(migrationBuilder: MigrationBuilder) |
+------------------------------------------------+
- Inheritance:
AddNewColumnsBillingβ’ inherits from β’Microsoft.EntityFrameworkCore.Migrations.Migration
8. Rollback Strategy π
-
Purpose: Ensures safe schema changes.
-
Procedure:
- Identify the last migration you want to retain (e.g.,
20230629180000_PreviousMigration). - Run:
dotnet ef database update 20230629180000_PreviousMigration - EF Core invokes Down in
AddNewColumnsBillingto drop the newly added columns.
- Identify the last migration you want to retain (e.g.,
9. Conclusion π―
This migration is a simple yet crucial schema update that empowers the billing module to capture carrier and employee information. By following EF Core best practices for Up/Down methods and leveraging the .NET CLI, you can smoothly integrate, test, and roll back these changes in your development or production environments.
Happy coding! π§βπ»π