πŸ“š Documentation: AddMeterColumnEquipmentEntry Migration


πŸ“‘ Table of Contents

  1. πŸ” Overview & Purpose
  2. πŸ› οΈ Key Functional Areas
  3. 🧩 Dependencies & Technologies
  4. πŸ“¦ Data Models & Schema Changes
  5. βš™οΈ Migration Methods (Up & Down)
  6. πŸƒ How to Apply & Rollback the Migration
  7. πŸ’‘ Usage Examples
  8. πŸ“ˆ UML Class Diagram
  9. πŸš€ 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 Meter of type double (float in SQL Server terms) to the EquipmentEntry table under the irby schema.
  • Ensure existing rows default this column to 0.0 to 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 MigrationBuilder APIs

🧩 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.0 ensures existing rows get a valid non-null value.

πŸƒ How to Apply & Rollback the Migration

  1. Add Migration (already done):

    dotnet ef migrations add AddMeterColumnEquipmentEntry \
        --context YourDbContextName \
        --project IrbyImaging.DbContext
    
  2. Update Database:

    dotnet ef database update \
        --context YourDbContextName \
        --project IrbyImaging.DbContext
    
  3. 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! πŸŽ‰