π Documentation: AddReferenceNumberToTimesheet Migration
File Path:
IrbyImaging.DbContext/Migrations/20240802133735_AddReferenceNumberToTimesheet.cs
Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Key Functional Areas
- Code Walkthrough
4.1. The Up Method
4.2. The Down Method - Database Schema Impact
- Usage & Sample Commands
- Best Practices & Error Handling
- UML Diagram
- Conclusion
Overview & Purpose
This Entity Framework Core migration AddReferenceNumberToTimesheet automates the addition of a new, non-nullable integer column named ReferenceNumber to the MechanicTimesheet table within the "irby" schema.
π Primary goals:
- Extend the data model to store a reference number for each timesheet.
- Ensure all existing records get a default value (
0) to maintain data integrity.
Dependencies & Technologies
| Technology / Package | Version | Purpose |
|---|---|---|
| .NET / C# | β₯ .NET 6.0 | Base platform |
| Microsoft.EntityFrameworkCore | β₯ 6.0.0 | ORM & Migration support |
| SQL Server (or compatible) | N/A | Target database |
Note: This migration was scaffolded by EF Coreβs
dotnet ef migrations addcommand.
Key Functional Areas
- Migration Class Definition
Declares a partial class inheriting fromMigration. - Up(MigrationBuilder)
Defines schema changes to apply (adding the new column). - Down(MigrationBuilder)
Provides a rollback path (dropping the column).
Code Walkthrough
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace IrbyImaging.DbContext.Migrations
{
/// <inheritdoc />
public partial class AddReferenceNumberToTimesheet : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "ReferenceNumber",
schema: "irby",
table: "MechanicTimesheet",
type: "int",
nullable: false,
defaultValue: 0);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "ReferenceNumber",
schema: "irby",
table: "MechanicTimesheet");
}
}
}
The Up Method
- Signature
protected override void Up(MigrationBuilder migrationBuilder) - Behavior
- Calls
migrationBuilder.AddColumn<int>(β¦). - Specifies:
name:"ReferenceNumber"schema:"irby"table:"MechanicTimesheet"type:"int"nullable:falsedefaultValue:0
- Calls
- Result
Adds a non-nullable integer column. All existing rows receive0by default.
The Down Method
- Signature
protected override void Down(MigrationBuilder migrationBuilder) - Behavior
- Calls
migrationBuilder.DropColumn(β¦). - Drops
ReferenceNumberfrom the specified schema/table.
- Calls
- Result
Reverts the schema to its previous state by removing the column.
Database Schema Impact
Before Migration
irby.MechanicTimesheet
ββββββββββ¬βββββββββββββββ¬ββββββββββ
β TimesheetId β MechanicId β Date β β¦
ββββββββββ΄βββββββββββββββ΄ββββββββββ
After Migration
irby.MechanicTimesheet
βββββββββββββββββ¬βββββββββββββββ¬ββββββββββ¬βββββββββββββββββ
β TimesheetId β MechanicId β Date β ReferenceNumber β
βββββββββββββββββΌβββββββββββββββΌββββββββββΌβββββββββββββββββ€
β 1 β 42 β β¦ β 0 β
βββββββββββββββββ΄βββββββββββββββ΄ββββββββββ΄βββββββββββββββββ
| Column | Type | Nullable | Default |
|---|---|---|---|
| ReferenceNumber | int | No | 0 |
Usage & Sample Commands
Adding & Applying the Migration
-
Scaffold (already done)
dotnet ef migrations add AddReferenceNumberToTimesheet \ --namespace IrbyImaging.DbContext.Migrations -
Update Database
dotnet ef database update
Entity Model Adjustment
After the migration, update your entity class:
namespace IrbyImaging.Models
{
public class MechanicTimesheet
{
public int TimesheetId { get; set; }
public int MechanicId { get; set; }
public DateTime Date { get; set; }
// π New property
public int ReferenceNumber { get; set; }
}
}
Sample Query
var sheet = await context.MechanicTimesheets
.Where(t => t.ReferenceNumber > 1000)
.ToListAsync();
Best Practices & Error Handling
- β Always test migrations in a staging environment before production.
- β Backup your database or use source-controlled migrations.
- β οΈ Dropping columns (in
Down) may cause data loss. Use with caution.
UML Diagram
ββββββββββββββββββββββββββββββββββββ
β Migration (EF Core) β
ββββββββββββββββββββββββββββββββββββ
β²
β
ββββββββββββββββββββββββββββββββββββ
β AddReferenceNumberToTimesheet β
β - inherits Migration β
β + Up(MigrationBuilder) β
β + Down(MigrationBuilder) β
ββββββββββββββββββββββββββββββββββββ
Conclusion
This migration adds robust tracking for each mechanicβs timesheet via a new ReferenceNumber column. By following EF Core best practicesβscaffolding, testing, and updating the data modelβyou maintain a consistent schema evolution workflow. π
Next Steps:
- Merge the migration into your main branch.
- Deploy to staging/production.
- Update any relevant business logic to leverage the new
ReferenceNumber.
Thank you for using IrbyImagingβs data-layer standards! π