π Documentation: AddMeterColumnEquipmentEntry Migration
π Table of Contents
- π Overview & Purpose
- π οΈ Key Functional Areas
- π§© Dependencies & Technologies
- π¦ Data Models & Schema Changes
- βοΈ Migration Methods (Up & Down)
- π How to Apply & Rollback the Migration
- π‘ Usage Examples
- π UML Class Diagram
- π Conclusion
π Overview & Purpose
The AddMeterColumnEquipmentEntry migration is part of the Entity Framework Core code-first approach within the IrbyImaging.DbContext project.
Purpose:
- Add a new column named
Meterof typedouble(float in SQL Server terms) to theEquipmentEntrytable under theirbyschema. - Ensure existing rows default this column to
0.0to maintain non-nullability. - Provide a Down method to cleanly remove the column if needed.
This migration helps track a measurement or βmeter readingβ for each equipment entry in the database, supporting new business requirements around equipment usage or calibration.
π οΈ Key Functional Areas
- Schema Modification
- Adding a non-nullable column
- Setting a default value
- Version Control
- Up/Down migration scripts to apply or revert changes
- Entity Framework Core Integration
- Leveraging
MigrationBuilderAPIs
- Leveraging
π§© Dependencies & Technologies
| Technology / Package | Version / Notes |
|---|---|
| .NET SDK | 7.0+ (C# 11) |
| Microsoft.EntityFrameworkCore | 7.x or above |
| Microsoft.EntityFrameworkCore.Relational | 7.x or above |
| Database Provider | Microsoft.EntityFrameworkCore.SqlServer |
π¦ Data Models & Schema Changes
Before Migration
Table: irby.EquipmentEntry
| Column Name | Data Type | Nullable | Default |
|---|---|---|---|
| Id | int | No | Identity |
| Name | nvarchar | No | |
| β¦ | β¦ | β¦ | β¦ |
After Migration
Table: irby.EquipmentEntry
| Column Name | Data Type | Nullable | Default |
|---|---|---|---|
| Meter | float | No | 0.0 |
| Id | int | No | Identity |
| Name | nvarchar | No | |
| β¦ | β¦ | β¦ | β¦ |
βοΈ Migration Methods (Up & Down)
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace IrbyImaging.DbContext.Migrations
{
/// <summary>
/// Adds a 'Meter' column to the 'EquipmentEntry' table (schema: irby).
/// </summary>
public partial class AddMeterColumnEquipmentEntry : Migration
{
/// <summary>
/// Implements the schema change: add Meter column with default 0.0.
/// </summary>
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<double>(
name: "Meter",
schema: "irby",
table: "EquipmentEntry",
type: "float",
nullable: false,
defaultValue: 0.0);
}
/// <summary>
/// Reverts the Up() method by dropping the Meter column.
/// </summary>
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "Meter",
schema: "irby",
table: "EquipmentEntry");
}
}
}
Key Points:
- Up: Adds the new column.
- Down: Removes it, ensuring the migration is reversible.
defaultValue: 0.0ensures existing rows get a valid non-null value.
π How to Apply & Rollback the Migration
-
Add Migration (already done):
dotnet ef migrations add AddMeterColumnEquipmentEntry \ --context YourDbContextName \ --project IrbyImaging.DbContext -
Update Database:
dotnet ef database update \ --context YourDbContextName \ --project IrbyImaging.DbContext -
Rollback (to previous migration):
dotnet ef database update LastGoodMigrationName
π‘ Usage Examples
1. Querying the New Column
using var context = new IrbyDbContext();
var entry = context.EquipmentEntries.FirstOrDefault(e => e.Id == 1);
Console.WriteLine($"Meter reading: {entry.Meter}");
2. Updating the Meter Value
using var context = new IrbyDbContext();
var entry = context.EquipmentEntries.Find(1);
entry.Meter = 123.45;
context.SaveChanges();
Console.WriteLine("Meter updated successfully!");
π UML Class Diagram
classDiagram
class Migration {
<<abstract>>
+ Up(MigrationBuilder)
+ Down(MigrationBuilder)
}
class AddMeterColumnEquipmentEntry {
+ Up(MigrationBuilder)
+ Down(MigrationBuilder)
}
AddMeterColumnEquipmentEntry --|> Migration
- Migration (abstract EF Core base class)
- AddMeterColumnEquipmentEntry inherits and implements schema changes.
π Conclusion
This migration is a simple yet essential step in evolving the EquipmentEntry data model to support a new Meter field.
By following EF Core conventions, it remains safe, reversible, and integrated with the overall code-first workflow.
Happy Coding! π