π IrbyImaging.DbContext/Migrations/20240701202611_UpdateMechanicTimesheet.cs Documentation
π Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Migration File Structure
- Key Functional Areas
- UML Class Diagram
- Applying & Rolling Back Migrations
- Sample Code Blocks
- Conclusion
1. Overview & Purpose
Filename:
20240701202611_UpdateMechanicTimesheet.cs
Namespace:
IrbyImaging.DbContext.Migrations
This Entity Framework Core migration adds a new column, EmployeeName, to the MechanicTimesheet table in the irby schema, and provides a way to remove it if the migration is rolled back.
| Aspect | Details |
|---|---|
| Migration Name | UpdateMechanicTimesheet |
| Target Table | irby.MechanicTimesheet |
| Column Added | EmployeeName (nvarchar(max), nullable) |
| Migrations Used | AddColumn, DropColumn |
| Created On | 2024-07-01 20:26:11 UTC |
2. Dependencies & Technologies
| Dependency/Tech | Purpose |
|---|---|
| Microsoft.EntityFrameworkCore.Migrations | Core migrations API for code-first database changes |
| C# (.NET 6+/7+) | Language & runtime for executing migrations |
π Note: Ensure your project references
Microsoft.EntityFrameworkCoreand has the properDbContextconfigured.
3. Migration File Structure
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace IrbyImaging.DbContext.Migrations
{
/// <inheritdoc />
public partial class UpdateMechanicTimesheet : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
// Adds EmployeeName column
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
// Drops EmployeeName column
}
}
}
Key Points:
- The class inherits from
Migration. - Two methods define the schema change:
Up(apply) andDown(revert). #nullable disableensures compatibility regardless of nullable reference settings.
4. Key Functional Areas
Up() Method β Applying the Change π§
Adds a new string column EmployeeName to the MechanicTimesheet table:
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "EmployeeName",
schema: "irby",
table: "MechanicTimesheet",
type: "nvarchar(max)",
nullable: true);
}
AddColumn<string>: Specifies a string (nvarchar(max)) column.nullable: true: The column allowsNULLvalues for backward compatibility.
What It Does
- Alters the database schema.
- Injects the new column into all existing and future
MechanicTimesheetrecords. - Does not enforce a default valueβexisting rows will have
EmployeeName = NULL.
Down() Method β Rolling Back π
Removes the previously added EmployeeName column:
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "EmployeeName",
schema: "irby",
table: "MechanicTimesheet");
}
DropColumn: Safely deletes the column if the migration is reversed.- Ensures the database can revert to its prior state.
5. UML Class Diagram
classDiagram
Migration <|-- UpdateMechanicTimesheet
class Migration {
<<abstract>>
+Up(MigrationBuilder)
+Down(MigrationBuilder)
}
class UpdateMechanicTimesheet {
+Up(MigrationBuilder)
+Down(MigrationBuilder)
}
Migrationis the base class provided by EF Core.UpdateMechanicTimesheetimplements theUpandDownoverrides.
6. Applying & Rolling Back Migrations
Applying the Migration
# From your project directory:
dotnet ef migrations add UpdateMechanicTimesheet
dotnet ef database update
ef migrations add: Generates a new migration file (timestamp + name).ef database update: Applies all pending migrations, includingUpdateMechanicTimesheet.
Rolling Back a Migration
# Target the previous migration (for example, β20240630_PreviousMigrationβ):
dotnet ef database update 20240630_PreviousMigration
- This command will invoke the
Downmethod ofUpdateMechanicTimesheetto removeEmployeeName.
7. Sample Code Blocks
Before Migration: MechanicTimesheet Model
public class MechanicTimesheet
{
public int Id { get; set; }
public DateTime Date { get; set; }
// No EmployeeName yet
public int HoursWorked { get; set; }
}
After Migration: MechanicTimesheet Model
public class MechanicTimesheet
{
public int Id { get; set; }
public DateTime Date { get; set; }
public string? EmployeeName { get; set; } // New nullable property
public int HoursWorked { get; set; }
}
8. Conclusion π―
- Purpose Achieved: Safely extends the
MechanicTimesheettable with an optionalEmployeeNamefield. - Reversible: Follows EF Core best practices with
Up/Downmethods for forward and backward compatibility. - Next Steps:
- Update your domain model to include
EmployeeName. - Rebuild and run your application to utilize the new column.
- Test creating, reading, and updating
MechanicTimesheetrecords with the new field.
- Update your domain model to include
π By following this migration pattern, your database evolves alongside your code, ensuring data integrity and application stability.
Happy coding & smooth deployments! ππ§